# Policy choice after Panel 1, for Study 2 in 
# Voters Use Campaign Finance Transparency and Compliance Information
# Abby K. Wood
# Political Behavior, 2022

# This file contains the code used to run the conjoint analysis. 
# It replicates Figures 5-8 in the manuscript, along with appendix figures and
# tables in Appendices 6-11 (except for 10A, which is in the study 1 code file).

rm(list = ls())
gc()

# set working directory
#setwd()

# setting seed
set.seed(54678)

#install.packages("cjoint")
#install.packages("cregg")
#install.packages("ggplot2")
#install.packages("plyr")
#install.packages("reshape2")
#install.packages("data.table")
#install.packages("httpuv", dependencies=TRUE)

library(httpuv)
library(cregg)
library(cjoint) # please remember to load cjoint after cregg or else cregg will not run properly
library(ggplot2)
library(grDevices)
library(plyr)
library(reshape2)
library(data.table)

load("finalcleanconjointdata.RData")

# APPENDIX 2 - RESPONDENT DEMOGRAPHICS (for column 3 ("Bovitz sample") of Table A2) ----

unique <- subset(data, select = c(ParticipantID, Gender, Age, Ethnicity, Income, 
                                  Hispanic, education, PIDdem, PIDrep, PIDlean, 
                                  PID1))
unique2 <- unique[!duplicated(unique),]


unique2$gender[unique2$Gender == 1] <- "Male"
unique2$gender[unique2$Gender == 2] <- "Female"
length(unique2$gender[unique2$gender == "Male"])/length(unique2$gender)
length(unique2$gender[unique2$gender == "Female"])/length(unique2$gender)



unique2$ethnicity[unique2$Ethnicity == 1 & unique2$Hispanic == 2] <- "White"
unique2$ethnicity[unique2$Ethnicity == 99] <- "Decline to state"
unique2$ethnicity[unique2$Ethnicity == 2] <- "Black"
unique2$ethnicity[unique2$Ethnicity == 3] <- "Asian"
unique2$ethnicity[unique2$Ethnicity == 4] <- "Native Hawai'ian or Pacific Islander"
unique2$ethnicity[unique2$Ethnicity == 5] <- "Native American"
unique2$ethnicity[unique2$Hispanic == 1] <- "Hispanic"
unique2$ethnicity[unique2$Ethnicity != 1 & unique2$Ethnicity != 99 & 
                    unique2$Ethnicity != 2 & unique2$Ethnicity != 3 &
                    unique2$Ethnicity != 4 & unique2$Ethnicity != 5 & 
                    unique2$Hispanic != 1] <- "Other or multiple"

length(unique2$ethnicity[unique2$ethnicity == "White"])/length(complete.cases(unique2$ethnicity))
length(unique2$ethnicity[unique2$ethnicity == "Black"])/length(complete.cases(unique2$ethnicity))
length(unique2$ethnicity[unique2$ethnicity == "Asian"])/length(complete.cases(unique2$ethnicity))
length(unique2$ethnicity[unique2$ethnicity == "Hispanic"])/length(complete.cases(unique2$ethnicity))
length(unique2$ethnicity[unique2$ethnicity == "Native Hawai'ian or Pacific Islander" | 
                           unique2$ethnicity == "Native American" | 
                           unique2$ethnicity == "Other or multiple"]) / length(complete.cases(unique2$ethnicity))


length(unique2$PID1[unique2$PIDdem == "Strong Democrat" | 
                     unique2$PIDdem == "Not very strong Democrat" | 
                     unique2$PIDlean == "Democratic Party"])/length(unique2$PID1)

length(unique2$PID1[unique2$PIDrep == "Strong Republican" | 
                      unique2$PIDrep == "Not very strong Republican" | 
                      unique2$PIDlean == "Republican Party"])/length(unique2$PID1)

length(unique2$PID1[unique2$PIDlean == "Neither"])/length(unique2$PID1)

unique2$Age <- as.numeric(unique2$Age)
length(which(unique2$Age < 30))/length(unique2$Age)
length(which(unique2$Age > 29 & unique2$Age< 40))/length(unique2$Age)
length(which(unique2$Age > 39 & unique2$Age< 50))/length(unique2$Age)
length(which(unique2$Age > 49 & unique2$Age< 60))/length(unique2$Age)
length(which(unique2$Age > 59))/length(unique2$Age)

unique2$income[unique2$Income == 1] <- "< $25,000"
unique2$income[unique2$Income == 2] <- "$25,000 - $34,999"
unique2$income[unique2$Income == 3] <- "$35,000 - $49,999"
unique2$income[unique2$Income == 4] <- "$50,000 - $74,999"
unique2$income[unique2$Income == 5] <- "$75,000 - $99,999"
unique2$income[unique2$Income == 6] <- "$100,000 - $124,999"
unique2$income[unique2$Income == 7] <- "$125,000 - $149,999"
unique2$income[unique2$Income == 8] <- "$150,000 - $174,999"
unique2$income[unique2$Income == 9] <- "$175,000 - $199,999"
unique2$income[unique2$Income == 10] <- "$200,000 - $249,999"
unique2$income[unique2$Income == 11] <- " > $250,000"
unique2$income[unique2$Income == 99] <- "Decline to state"

length(which(unique2$Income < 4))/length(unique2$income[unique2$Income != 99])
length(which(unique2$Income > 3 & unique2$Income < 6))/length(unique2$income[unique2$Income != 99])
length(which(unique2$Income > 5 & unique2$Income < 8))/length(unique2$income[unique2$Income != 99])
length(which(unique2$Income == 8 | unique2$Income == 9))/length(unique2$income[unique2$Income != 99])
length(which(unique2$Income == 10 | unique2$Income == 11))/length(unique2$income[unique2$Income != 99])



length(which(unique2$education == 1))/length(unique2$education)
length(which(unique2$education == 2))/length(unique2$education)
length(which(unique2$education == 3 | unique2$education == 4 | unique2$education == 5))/length(unique2$education)
length(which(unique2$education > 5))/length(unique2$education)

# CODING FOR CONJOINT BEGINS HERE ----

# note: on issue importance, 1 is MOST important 
data$Party <- as.factor(data$party_assigned)

# quick clean
data$PID <- NA
data$PID[data$PIDrep == "Not very strong Republican"] <- 2
data$PID[data$PIDrep == "Strong Republican"] <- 1
data$PID[data$PIDdem == "Not very strong Democrat"] <- 6
data$PID[data$PIDdem == "Strong Democrat"] <- 7
data$PID[data$PIDlean == "Democratic Party"] <- 5
data$PID[data$PIDlean == "Republican Party"] <- 3
data$PID[(data$PID1 == "Independent" | data$PID1 == "No preference" | data$PID1 == "Other") & 
           data$PIDlean == "Neither"] <- 4
data$PID <- as.factor(data$PID)


# this has to come before factor
data$persuasiveness[data$persuasiveness == "Extremely Persuasive"] <- "Extremely persuasive"

data$`Police Immigrant` <- as.factor(data$police_immigrant)
data$`Sex Ed` <- as.factor(data$sex_ed)
data$`CF Compliance` <- as.factor(data$cf_compliance)
data$`Total amount raised` <- as.factor(data$`Total amount raised`)
data$`Small Donors` <- as.factor(data$small_donors)
data$`Professional Background` <- as.factor(data$`Professional Background`)
data$Persuasiveness <- as.factor(data$persuasiveness)
data$`Grasp Issues` <- as.factor(data$grasp)
data$`Dark Money` <- as.factor(data$relationship_dmgroups)
data$`Party Assigned` <- as.factor(data$party_assigned)

data$engaged <- as.numeric(data$newsint == 1)

# note: pooling over and under compliance because the overall results show they aren't treated differently by 
# respondents
  
data$`CF Pooled Compliance` <- NA
data$`CF Pooled Compliance`[data$`CF Compliance` == "donors_on_site" | 
                              data$`CF Compliance` == "map_on_site"] <- "overcomplied"
data$`CF Pooled Compliance`[data$`CF Compliance` == "in_compliance"] <- "complied"
data$`CF Pooled Compliance`[data$`CF Compliance` == "reporting_gaps" | 
                              data$`CF Compliance` == "missed_deadlines"] <- "noncompliant"
data$`CF Pooled Compliance` <- as.factor(data$`CF Pooled Compliance`)

datasmall <- subset(data, select = c(Party, `Police Immigrant`, `Sex Ed`, `CF Compliance`, 
                                     `Total amount raised`, `Small Donors`, `Professional Background`, 
                                     Persuasiveness, `Grasp Issues`, `Dark Money`, 
                                     chosen, ParticipantID, `CF Pooled Compliance`, `Party Assigned`, PID, ideo7, 
                                     contest_no, Candidate, SupSexEd, SupImmigPolicy, trustrating))

datasmall$randomized_independents <- as.numeric(datasmall$PID == 4 & (datasmall$ideo7 == "Moderate; Middle of the Road" |
                                                                        datasmall$ideo7 == "Haven't thought much about this"))

datasmall2 <- subset(datasmall, randomized_independents == 0)

# create attribute list for conjoint analysis
attribute_list <- list()
attribute_list[["Party"]] <- c("D", "R")  
attribute_list[["Police Immigrant"]] <- c("Opposes", "Strongly opposes",  "Supports", "Strongly Supports")
attribute_list[["`Sex Ed`"]] <-  c("Opposes", "Strongly opposes",  "Supports", "Strongly Supports")
attribute_list[["CF Compliance"]] <- c("in_compliance", "missed_deadlines", "reporting_gaps", 
                                       "donors_on_site", "map_on_site")
attribute_list[["Dark Money"]] <- 
  c("dark_money_support", "discouraged_dark_money", "raised_dark_money")
attribute_list[["Total amount raised"]] <- c("$250,000", "$500,000", "$750,000")
attribute_list[["Small Donors"]] <- c("40%", "60%", "80%")
attribute_list[["Professional Background"]] <- c("Student", "Head of State Agency", 
                                                 "Mayor", "Small Business Owner", 
                                                 "School Principal")
attribute_list[["Persuasiveness"]] <- 
  c("Somewhat persuasive", "Not very persuasive",  "Extremely persuasive")
attribute_list[["Grasp Issues"]] <- 
  c("Good grasp", "Weak grasp",  "Excellent grasp")
attribute_list[["CF Pooled Compliance"]] <- c("complied", "overcomplied", "noncompliant")


# constraint list 
constraint_list <- list()
constraint_list[[1]] <- list()
# can't have Democratic candidate strongly oppose these policies
constraint_list[[1]][["Party"]] <- "D"
constraint_list[[1]][["Police Immigrant"]] <- "Strongly opposes"
constraint_list[[1]][["`Sex Ed`"]] <- "Strongly opposes"
# can't have Republican candidate strongly support these policies
constraint_list[[2]] <- list()
constraint_list[[2]][["Party"]] <- "R"
constraint_list[[2]][["Police Immigrant"]] <- "Strongly Supports"
constraint_list[[2]][["`Sex Ed`"]] <- "Strongly Supports"

# create baselines
baselines <- list()
baselines$`CF Compliance` <- "in_compliance"
baselines$`CF Pooled Compliance` <- "complied"
baselines$`Dark Money` <- "dark_money_support"
baselines$`Police Immigrant` <- "Opposes"
baselines$`Sex Ed` <- "Opposes"
baselines$`Professional Background` <- "Head of State Agency"
baselines$`Grasp Issues` <- "Good grasp"
baselines$Persuasiveness <- "Somewhat persuasive"
# these two are baselines that are commented out because the cjoint() package wasn't playing nicely with them; 
# it's fine because they end up being the default baselines anyway w/o need to tell cjoint() to use them.
#baselines$smalldonors <- "40"
#baselines$Totalamountraised <- "$250,000"
# an alternative version I tried, which also didn't work...
#baselines$`Small Donors` <- "40"
#baselines$`Total amount raised` <- "$250,000"


# create design for conjoint
design <- makeDesign(type = "constraints", 
                      attribute.levels = attribute_list, 
                     constraints = constraint_list)

# overall results 
myresults <- amce(chosen ~ `CF Compliance` + `Grasp Issues` + Persuasiveness + 
                    `Professional Background` +
                    `Dark Money` + `Small Donors` + `Total amount raised` +
                    `Police Immigrant` + `Sex Ed`, data = datasmall,
                  respondent.id = "ParticipantID", cluster = TRUE, design = design,
                  baselines = baselines)

group.order <- c( "Dark Money", "CF Compliance", 
                  "Persuasiveness", "Grasp Issues", 
                 "Total amount raised", "Small Donors", "Professional Background",
                 "Police Immigrant", "Sex Ed")

# FIGURE 5, OVERALL RESULTS FROM CONJOINT ----

plot(myresults, main = "", xlab = "Change in E[Y]",
     ci = 0.95, colors = NULL, 
     xlim = NULL, breaks = NULL,
     labels = NULL, attribute.names = NULL,
     level.names = NULL,
     label.baseline = TRUE, text.size = 7, text.color = "black",
     point.size = 0.5, dodge.size = 0.9, plot.theme = NULL,
     plot.display = "all",
     facet.names = NULL, facet.levels = NULL,
     group.order = group.order,
     legend.position="none")


# Claims in texts:  
  # "The AMCE for raised dark money (AMCE = 0.01 95% CI = [-0.01, 0.03]) is indistinguishable from 
  # supported by dark money, the baseline level"; 

  # "Respondents are 12 percentage points less likely to choose a candidate who missed deadlines 
  # (-0.12, [-0.14, -0.10]) or had missing contributions (-0.12, [-0.14, -0.10]). 

  # "Discouraged dark money is positive and statistically distinguishable from the baseline of 
  # supported by dark money (0.062, [0.04, 0.08]). H2b, that respondents will reward candidate over-disclosure, 
  # is marginally supported in the overall results, with the website map (0.026, 95% CI [0.002, 0.05]) 
  # distinguishable from “merely” being in compliance (baseline level), but website information about small donors 
  # not distinguishable (0.02, [-0.01, 0.04]). 

summary(myresults)  # see estimate and std.errors for "raised_dark_money"; also "missed_deadlines" and 
                    # "reporting_gaps"; 
                    # also "discouraged_dark_money", "donors_on_site", and "map_on_site".



## Footnote 17, stability with no carryover effects and no profile order effects ----
  # Stability with no carryover effects first.  To test this, look at each of 
  # the tasks separately to make sure that the estimates for task 1 ... task 6 
  # are all similar.
    
    attribute_list2 <- list()
    attribute_list2[["Party"]] <- c("D", "R")  
    attribute_list2[["Police Immigrant"]] <- c("Opposes", "Strongly opposes",  "Supports", "Strongly Supports")
    attribute_list2[["`Sex Ed`"]] <-  c("Opposes", "Strongly opposes",  "Supports", "Strongly Supports")
    attribute_list2[["CF Compliance"]] <- c("in_compliance","donors_on_site", "map_on_site",  
                                            "missed_deadlines", "reporting_gaps")
    attribute_list2[["Dark Money"]] <- 
      c("dark_money_support", "discouraged_dark_money", "raised_dark_money")
    attribute_list2[["Total amount raised"]] <- c("$250,000", "$500,000", "$750,000")
    attribute_list2[["Small Donors"]] <- c("40%", "60%", "80%")
    attribute_list2[["Professional Background"]] <- c("Student", "Head of State Agency", 
                                                      "Mayor", "Small Business Owner", 
                                                      "School Principal")
    attribute_list2[["Persuasiveness"]] <- 
      c("Somewhat persuasive", "Not very persuasive",  "Extremely persuasive")
    attribute_list2[["Grasp Issues"]] <- 
      c("Good grasp", "Weak grasp",  "Excellent grasp")
    attribute_list2[["CF Pooled Compliance"]] <- c("complied", "overcomplied", "noncompliant")
    attribute_list2[["contest_number"]] <- c("1", "2", "3", "4", "5", "6")
    
    # constraint list for conjoint attributes
    constraint_list <- list()
    constraint_list[[1]] <- list()
    # can't have D strongly oppose these policies
    constraint_list[[1]][["Party"]] <- "D"
    constraint_list[[1]][["Police Immigrant"]] <- "Strongly opposes"
    constraint_list[[1]][["`Sex Ed`"]] <- "Strongly opposes"
    # can't have R strongly support these policies
    constraint_list[[2]] <- list()
    constraint_list[[2]][["Party"]] <- "R"
    constraint_list[[2]][["Police Immigrant"]] <- "Strongly Supports"
    constraint_list[[2]][["`Sex Ed`"]] <- "Strongly Supports"
    
    
    baselines <- list()
    #baselines$`CF Compliance` <- "in_compliance"
    baselines$`CF Pooled Compliance` <- "complied"
    baselines$`Dark Money` <- "dark_money_support"
    baselines$`Police Immigrant` <- "Opposes"
    baselines$`Sex Ed` <- "Opposes"
    baselines$`Professional Background` <- "Head of State Agency"
    baselines$`Grasp Issues` <- "Good grasp"
    baselines$Persuasiveness <- "Somewhat persuasive"
    #baselines$smalldonors <- "40"
    #baselines$Totalamountraised <- "$250,000"
    
    # conjoint design 2
    design2 <- makeDesign(type = "constraints", 
                          attribute.levels = attribute_list2, 
                          constraints = constraint_list)
    
    datasmall$contest_number <- as.factor(datasmall$contest_no)
    carryover <- amce(chosen ~ contest_number : `CF Pooled Compliance` + 
                        contest_number : `Grasp Issues` + 
                        contest_number : Persuasiveness + 
                        contest_number : `Professional Background` +
                        contest_number : `Dark Money` + 
                        contest_number : `Small Donors` + 
                        contest_number : `Total amount raised` +
                        contest_number : `Police Immigrant` + 
                        contest_number : `Sex Ed`, data = datasmall, 
                      respondent.id = "ParticipantID", cluster = TRUE, design=design2, 
                      baselines = baselines)
    
    summary(carryover)  # note that only 5 of 138 interactions with contest 
    # number were significantly different from the first contest using 
    # alpha = 0.05. (From footnote 17)
    
  ## Now testing profile order effects (still in FN 17)
    attribute_list3 <- list()
    attribute_list3[["Party"]] <- c("D", "R")  
    attribute_list3[["Police Immigrant"]] <- c("Opposes", "Strongly opposes",  "Supports", "Strongly Supports")
    attribute_list3[["`Sex Ed`"]] <-  c("Opposes", "Strongly opposes",  "Supports", "Strongly Supports")
    attribute_list3[["CF Compliance"]] <- c("in_compliance","donors_on_site", "map_on_site",  
                                            "missed_deadlines", "reporting_gaps")
    attribute_list3[["Dark Money"]] <- 
      c("dark_money_support", "discouraged_dark_money", "raised_dark_money")
    attribute_list3[["Total amount raised"]] <- c("$250,000", "$500,000", "$750,000")
    attribute_list3[["Small Donors"]] <- c("40%", "60%", "80%")
    attribute_list3[["Professional Background"]] <- c("Student", "Head of State Agency", 
                                                      "Mayor", "Small Business Owner", 
                                                      "School Principal")
    attribute_list3[["Persuasiveness"]] <- 
      c("Somewhat persuasive", "Not very persuasive",  "Extremely persuasive")
    attribute_list3[["Grasp Issues"]] <- 
      c("Good grasp", "Weak grasp",  "Excellent grasp")
    attribute_list3[["CF Pooled Compliance"]] <- c("complied", "overcomplied", "noncompliant")
    attribute_list3[["Candidate"]] <- c("Candidate A", "Candidate B")
    
    # constraint list
    constraint_list <- list()
    constraint_list[[1]] <- list()
    # can't have D strongly oppose these policies
    constraint_list[[1]][["Party"]] <- "D"
    constraint_list[[1]][["Police Immigrant"]] <- "Strongly opposes"
    constraint_list[[1]][["`Sex Ed`"]] <- "Strongly opposes"
    # can't have R strongly support these policies
    constraint_list[[2]] <- list()
    constraint_list[[2]][["Party"]] <- "R"
    constraint_list[[2]][["Police Immigrant"]] <- "Strongly Supports"
    constraint_list[[2]][["`Sex Ed`"]] <- "Strongly Supports"
    
    
    baselines <- list()
    #baselines$`CF Compliance` <- "in"
    baselines$`CF Pooled Compliance` <- "complied"
    baselines$`Dark Money` <- "dark_money_support"
    baselines$`Police Immigrant` <- "Opposes"
    baselines$`Sex Ed` <- "Opposes"
    baselines$`Professional Background` <- "Head of State Agency"
    baselines$`Grasp Issues` <- "Good grasp"
    baselines$Persuasiveness <- "Somewhat persuasive"
    #baselines$smalldonors <- "40"
    #baselines$Totalamountraised <- "$250,000"
    
    
    design3 <- makeDesign(type = "constraints", 
                          attribute.levels = attribute_list3, 
                          constraints = constraint_list)
    
    datasmall$Candidate <- as.factor(datasmall$Candidate)
    profileorder <- amce(chosen ~ Candidate : `CF Pooled Compliance` + 
                           Candidate : `Grasp Issues` + 
                           Candidate : Persuasiveness + 
                           Candidate : `Professional Background` +
                           Candidate : `Dark Money` + 
                           Candidate : `Small Donors` + 
                           Candidate : `Total amount raised` +
                           Candidate : `Police Immigrant` + 
                           Candidate : `Sex Ed`, data = datasmall, 
                         respondent.id = "ParticipantID", cluster = TRUE, design=design3, 
                         baselines = baselines)
    
    summary(profileorder)  # observe from the Average Component Interaction Effects 
    # (ACIE) that there are no profile order effects here -- only 1 of these 
    # ACIE estimates is statistically distinguishable from zero.  
    # (This is mentioned in footnote 17)
    
    
    
## Claim in text:  "When pooled, the AMCE for overcompliance is distinguishable from 0 (0.02, [0.001, 0.04])." ----
    
    # pooling compliance 
    baselines2 <- list()
    #baselines2$`CF Compliance` <- "in_compliance"
    baselines2$`CF Pooled Compliance` <- "complied"
    baselines2$`Dark Money` <- "dark_money_support"
    baselines2$`Police Immigrant` <- "Opposes"
    baselines2$`Sex Ed` <- "Opposes"
    baselines2$`Professional Background` <- "Head of State Agency"
    baselines2$`Grasp Issues` <- "Good grasp"
    baselines2$Persuasiveness <- "Somewhat persuasive"
    
    group.order2 <- c("Dark Money", "CF  Pooled Compliance", 
                      "Persuasiveness", "Grasp Issues", 
                      "Total amount raised", "Small Donors", "Professional Background",
                      "Police Immigrant", "Sex Ed")
    
    myresults2 <- amce(chosen ~ `CF Pooled Compliance` + `Grasp Issues` + Persuasiveness + 
                         `Professional Background` +
                         `Dark Money` + `Small Donors` + `Total amount raised` +
                         `Police Immigrant` + `Sex Ed`, data = datasmall, 
                       respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                       baselines = baselines2)
    
    plot(myresults2, main = "", xlab = "Change in E[Y]",
         ci = 0.95, colors = NULL, 
         xlim = NULL, breaks = NULL,
         labels = NULL, attribute.names = NULL,
         level.names = NULL,
         label.baseline = TRUE, text.size = 8, text.color = "black",
         point.size = 0.5, dodge.size = 0.9, plot.theme = NULL,
         plot.display = "all",
         facet.names = NULL, facet.levels = NULL,
         group.order = group.order2)
    
    summary(myresults2) #shows that pooled overcompliance is statistically significant

## APPENDIX A6A - Replicating Figure 5 without independents ----
# (Mentioned in FN 14)
myresults_noind <- amce(chosen ~ `CF Compliance` + `Grasp Issues` + Persuasiveness + 
                    `Professional Background` +
                    `Dark Money` + `Small Donors` + `Total amount raised` +
                    `Police Immigrant` + `Sex Ed`, data = datasmall2, 
                  respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                  baselines = baselines)
    
plot(myresults_noind, main = "", xlab = "Change in E[Y]",
     ci = 0.95, colors = NULL, 
     xlim = NULL, breaks = NULL,
     labels = NULL, attribute.names = NULL,
     level.names = NULL,
     label.baseline = TRUE, text.size = 8, text.color = "black",
     point.size = 0.5, dodge.size = 0.9, plot.theme = NULL,
     plot.display = "all",
     facet.names = NULL, facet.levels = NULL,
     group.order = group.order,
     legend.position="none")


# FIGURES 6, A6B, A7A, Table A7B: RESULTS BY ISSUE DISTANCE ----

# These data & analyses are presented by issue area, starting with sex ed -- 
  
  # create "supse", respondent's support for sex ed
  data$supse <- NA
  data$supse[data$SupSexEd == "Strongly Support"] <- 4
  data$supse[data$SupSexEd == "Support"] <- 3
  data$supse[data$SupSexEd == "Oppose"] <- 2
  data$supse[data$SupSexEd == "Strongly Oppose"] <- 1
  
  # candidate sex ed support
  data$csupse <- NA
  data$csupse[data$`Sex Ed` == "Strongly Supports"] <- 4
  data$csupse[data$`Sex Ed` == "Supports"] <- 3
  data$csupse[data$`Sex Ed` == "Opposes"] <- 2
  data$csupse[data$`Sex Ed` == "Strongly opposes"] <- 1
  
  data$`issue distance sexed` <- abs(data$supse - data$csupse)
  
  datasmall <- as.data.frame(cbind(data$`issue distance sexed`, datasmall))
  names(datasmall)[1] <- "issue distance sexed"
  datasmall_sexed <- datasmall
  datasmall_sexed$`issue distance sexed` <- as.factor(datasmall_sexed$`issue distance sexed`)
  
  results.rvint.sexed <- amce(chosen ~ `issue distance sexed` : `Grasp Issues` + 
                                `issue distance sexed` : Persuasiveness + 
                                `issue distance sexed` : `Professional Background` +
                                `issue distance sexed` : `Dark Money` + 
                                `issue distance sexed` : `Small Donors` + 
                                `issue distance sexed` : `Total amount raised` +
                                `issue distance sexed` : `Police Immigrant` + 
                                `issue distance sexed` : `Sex Ed` + 
                                `issue distance sexed` : `CF Pooled Compliance`, 
                              data = datasmall_sexed,  respondent.id = "ParticipantID", cluster = T, 
                              design = design, baselines = baselines2, 
                              respondent.varying = "`issue distance sexed`")
  
  
  ## APPENDIX Figure A7A, sex ed panel ----
  plot(results.rvint.sexed, main = "", xlab = "Change in E[Y]",
       ci = 0.95, colors = NULL, 
       xlim = NULL, breaks = NULL,
       labels = NULL, attribute.names = NULL,
       level.names = NULL,
       label.baseline = F, text.size = 8, text.color = "black",
       point.size = 0.5, dodge.size = 0.9, plot.theme = NULL,
       plot.display = "interaction",
       facet.names = NULL, facet.levels = NULL,
       group.order = group.order2)
  
  
  ## Table A7B, sex ed results ----
  # each column is issue distance.
  amce.ID0.est <- round(summary(results.rvint.sexed)$issuedistancesexed1amce$Estimate, digits = 2)
  amce.ID0.SE.cilow <- round(summary(results.rvint.sexed)$issuedistancesexed1amce$Estimate, digits = 2) - 
    2*(summary(results.rvint.sexed)$issuedistancesexed1amce$`Std. Err`)
  amce.ID0.SE.cihigh <- round(summary(results.rvint.sexed)$issuedistancesexed1amce$Estimate, digits = 2) +
    2*(summary(results.rvint.sexed)$issuedistancesexed1amce$`Std. Err`)
  amce.ID0.CI.part1 <- paste("[", round(amce.ID0.SE.cilow, digits = 2), sep = "")
  amce.ID0.CI.part2 <- paste(round(amce.ID0.SE.cihigh, digits = 2), "]", sep = "")
  amce.ID0.CI <- paste(amce.ID0.CI.part1, amce.ID0.CI.part2, sep = ",")
  
  
  amce.ID1.est <- round(summary(results.rvint.sexed)$issuedistancesexed2amce$Estimate, digits = 2)
  amce.ID1.SE.cilow <- round(summary(results.rvint.sexed)$issuedistancesexed2amce$Estimate, digits = 2) - 
    2*(summary(results.rvint.sexed)$issuedistancesexed2amce$`Std. Err`)
  amce.ID1.SE.cihigh <- round(summary(results.rvint.sexed)$issuedistancesexed2amce$Estimate, digits = 2) +
    2*(summary(results.rvint.sexed)$issuedistancesexed2amce$`Std. Err`)
  amce.ID1.CI.part1 <- paste("[", round(amce.ID1.SE.cilow, digits = 2), sep = "")
  amce.ID1.CI.part2 <- paste(round(amce.ID1.SE.cihigh, digits = 2), "]", sep = "")
  amce.ID1.CI <- paste(amce.ID1.CI.part1, amce.ID1.CI.part2, sep = ",")
  
  
  amce.ID2.est <- round(summary(results.rvint.sexed)$issuedistancesexed3amce$Estimate, digits = 2)
  amce.ID2.SE.cilow <- round(summary(results.rvint.sexed)$issuedistancesexed3amce$Estimate, digits = 2) - 
    2*(summary(results.rvint.sexed)$issuedistancesexed3amce$`Std. Err`)
  amce.ID2.SE.cihigh <- round(summary(results.rvint.sexed)$issuedistancesexed3amce$Estimate, digits = 2) +
    2*(summary(results.rvint.sexed)$issuedistancesexed3amce$`Std. Err`)
  amce.ID2.CI.part1 <- paste("[", round(amce.ID2.SE.cilow, digits = 2), sep = "")
  amce.ID2.CI.part2 <- paste(round(amce.ID2.SE.cihigh, digits = 2), "]", sep = "")
  amce.ID2.CI <- paste(amce.ID2.CI.part1, amce.ID2.CI.part2, sep = ",")
  
  
  amce.ID3.est <- round(summary(results.rvint.sexed)$issuedistancesexed4amce$Estimate, digits = 2)
  amce.ID3.SE.cilow <- round(summary(results.rvint.sexed)$issuedistancesexed4amce$Estimate, digits = 2) - 
    2*(summary(results.rvint.sexed)$issuedistancesexed4amce$`Std. Err`)
  amce.ID3.SE.cihigh <- round(summary(results.rvint.sexed)$issuedistancesexed4amce$Estimate, digits = 2) +
    2*(summary(results.rvint.sexed)$issuedistancesexed4amce$`Std. Err`)
  amce.ID3.CI.part1 <- paste("[", round(amce.ID3.SE.cilow, digits = 2), sep = "")
  amce.ID3.CI.part2 <- paste(round(amce.ID3.SE.cihigh, digits = 2), "]", sep = "")
  amce.ID3.CI <- paste(amce.ID3.CI.part1, amce.ID3.CI.part2, sep = ",")
  
  amce.sexed.attributes <- summary(results.rvint.sexed)$issuedistancesexed1amce$Level
  
  ### All sex ed-related results for table A7B ----
  sexed.issuedist.table <- as.data.frame(cbind(amce.sexed.attributes, amce.ID0.est, amce.ID0.CI, 
                                               amce.ID1.est, amce.ID1.CI, 
                                               amce.ID2.est, amce.ID2.CI, 
                                               amce.ID3.est, amce.ID3.CI))
  
  

## FIGURE 6, sex ed panel ----
  
  # Now use sexed.issuedist.table to create Fig 6 (sex ed panel)
  fig6new <- sexed.issuedist.table[sexed.issuedist.table$amce.sexed.attributes == "overcomplied" |
                                     sexed.issuedist.table$amce.sexed.attributes == "noncompliant" |
                                     sexed.issuedist.table$amce.sexed.attributes == "discouraged_dark_money"|
                                     sexed.issuedist.table$amce.sexed.attributes == "raised_dark_money",]
  
  sexed.issuedist.table.cis <- subset(fig6new, select = c(amce.sexed.attributes,
                                                          amce.ID0.CI, amce.ID1.CI,
                                                          amce.ID2.CI,amce.ID3.CI))
  # get ci high and ci low
  # ID0
  amce.ID0.CI <- sexed.issuedist.table.cis$amce.ID0.CI
  amce.ID0.CI <- gsub("\\[|\\]", "", amce.ID0.CI)
  ID0.cilow <- substr(amce.ID0.CI, 1, 5)
  ID0.cilow <- gsub("\\,", "", ID0.cilow)
  ID0.cihigh <- substr(amce.ID0.CI, 6, 10)
  ID0.cihigh <- gsub("\\,", "", ID0.cihigh)
  
  # ID1
  amce.ID1.CI <- sexed.issuedist.table.cis$amce.ID1.CI
  amce.ID1.CI <- gsub("\\[|\\]", "", amce.ID1.CI)
  ID1.cilow <- substr(amce.ID1.CI, 1, 5)
  ID1.cilow <- gsub("\\,", "", ID1.cilow)
  ID1.cihigh <- substr(amce.ID1.CI, 6, 10)
  ID1.cihigh <- gsub("\\,", "", ID1.cihigh)
  ID1.cihigh[3] <- "-0.07"
  
  # ID2
  amce.ID2.CI <- sexed.issuedist.table.cis$amce.ID2.CI
  amce.ID2.CI <- gsub("\\[|\\]", "", amce.ID2.CI)
  ID2.cilow <- substr(amce.ID2.CI, 1, 5)
  ID2.cilow <- gsub("\\,", "", ID2.cilow)
  ID2.cihigh <- substr(amce.ID2.CI, 6, 10)
  ID2.cihigh <- gsub("\\,", "", ID2.cihigh)
  ID2.cilow[1] <- "0.00"
  ID2.cihigh[1] <- "0.08"
  
  # ID3
  amce.ID3.CI <- sexed.issuedist.table.cis$amce.ID3.CI
  amce.ID3.CI <- gsub("\\[|\\]", "", amce.ID3.CI)
  ID3.cilow <- substr(amce.ID3.CI, 1, 5)
  ID3.cilow <- gsub("\\,", "", ID3.cilow)
  ID3.cihigh <- substr(amce.ID3.CI, 6, 10)
  ID3.cihigh <- gsub("\\,", "", ID3.cihigh)
  
  # save to merge with reshaped data 
  amce.cihigh <- cbind(ID0.cihigh, ID1.cihigh, ID2.cihigh, ID3.cihigh)
  amce.cilow <- cbind(ID0.cilow, ID1.cilow, ID2.cilow, ID3.cilow)
  sexed.issuedist.table.new <- cbind(fig6new, amce.cihigh, amce.cilow)
  
  # get rid of the pasted CIs now
  sexed.issuedist.table.new <- subset(sexed.issuedist.table.new, 
                                      select = -c(amce.ID0.CI, amce.ID1.CI, 
                                                  amce.ID2.CI, amce.ID3.CI))
  
  
  # reshape
  f6long <- reshape2::melt(sexed.issuedist.table.new, id = 'amce.sexed.attributes', 
                           value.name = "value")
  
  # create index vectors to sort by (these may be extraneous)
  f6long$order_atts <- rep(c(3, 4, 1, 2), times = 12)
  f6long$order_IDs <- rep(c(rep(1, times = 4), rep(2, times = 4), rep(3, times = 4),
                            rep(4, times = 4)), times = 3)
  
  f6long <- f6long[order(f6long$order_atts, f6long$order_IDs),]
  
  f6long$`Policy Distance` <- rep(c(rep("0", times = 3), rep("1", times = 3),
                                    rep("2", times = 3), rep("3", times = 3)), 
                                  times = 4)
  
  f6long$stat <- rep(c("estimate", "ci.high", "ci.low"), times = 16)
  
  
  f6longtmp <- subset(f6long, select = c(amce.sexed.attributes, value, `Policy Distance`, stat))
  f6wide <- reshape2::dcast(f6longtmp, amce.sexed.attributes + `Policy Distance` ~ stat, value.var = "value")
  
  # now just re-sort f6wide to get it to go from ext cons to ext lib again and 
  # in attribute order.
  f6wide$distanceorder <- rep(c(4, 3, 2, 1), times = 4)
  f6wide$attributeorder <- c(rep(2, times = 4), rep(4, times = 4), rep(3, times = 4), rep(1, times = 4))
  f6wide <- f6wide[order(f6wide$attributeorder, f6wide$distanceorder),]
  f6wide$reorder <- index(1:length(f6wide$attributeorder))
  f6wide$addtoy <- c(0, 0, 0, 0, .3, .3, .3, .3, .7, .7, .7, .7, .9, .9, .9, .9)
  f6wide$ci.low <- as.numeric(f6wide$ci.low)
  f6wide$ci.high <- as.numeric(f6wide$ci.high)
  f6wide$pch <- rep(c(18, 17, 16, 15), times = 4)

  # plot initialization
    par(mar=c(5, 1, 4, 2))
    plot(0,0 , xlim=c(-0.3, 0.25) , ylim=c(0,4.5), col="white" , yaxt="n" , ylab="", 
         xlab="", xaxt = "n")
    
    # fill the graph
    for (i in 1:length(f6wide$estimate)){
      points(x=f6wide$estimate[i], y=((f6wide$reorder[i]*0.1)+(f6wide$reorder[i]/8) + f6wide$addtoy[i]), 
             pch = f6wide$pch[i])
      
      segments(x0 = f6wide$ci.low[i], y0 = ((f6wide$reorder[i]*0.1)+(f6wide$reorder[i]/8)+ f6wide$addtoy[i]), 
               x1 = f6wide$ci.high[i], y1 = ((f6wide$reorder[i]*0.1)+(f6wide$reorder[i]/8)+ f6wide$addtoy[i]))
    }
    #  add vertical line at 0
    segments(x0 = 0, x1 = 0, y0 = 0, y1 = 4.5, lty = 2, col = "black")
    
    # add / format axes
    #axis(2, at = c(0.5, 2, 3.5, 4.9), labels = unique(f6wide$amce.sexed.attributes), 
    #     tick = TRUE, col = "black", las = 1, cex.axis = 0.8)
    axis(1, at = c(-0.2, -0.1, 0.0, 0.1, 0.2), tick = TRUE, 
         cex.axis = 0.8)
    
    legend("topright", legend = c("0", "1", "2", "3"), 
           pch = c(15, 16, 17, 18), 
           title = "|Policy Distance|", cex = 0.65, ncol = 1)
    
    mtext("Change in E[Y]", side = 1, line=2, cex = 0.7, at = c(0,0))


    ### Fig A7A replication of Figure 6 w/o independents, sex ed panel only ----
    
    # create "a", the issue-specific distance measure of distance between r and c
    # first sex ed
    
    # respondent sex ed support
    datasmall2$supse <- NA
    datasmall2$supse[datasmall2$SupSexEd == "Strongly Support"] <- 4
    datasmall2$supse[datasmall2$SupSexEd == "Support"] <- 3
    datasmall2$supse[datasmall2$SupSexEd == "Oppose"] <- 2
    datasmall2$supse[datasmall2$SupSexEd == "Strongly Oppose"] <- 1
    
    # candidate sex ed support
    datasmall2$csupse <- NA
    datasmall2$csupse[datasmall2$`Sex Ed` == "Strongly Supports"] <- 4
    datasmall2$csupse[datasmall2$`Sex Ed` == "Supports"] <- 3
    datasmall2$csupse[datasmall2$`Sex Ed` == "Opposes"] <- 2
    datasmall2$csupse[datasmall2$`Sex Ed` == "Strongly opposes"] <- 1
    
    datasmall2$`issue distance sexed` <- abs(datasmall2$supse - datasmall2$csupse)
    
    
    datasmall2 <- as.data.frame(cbind(datasmall2$`issue distance sexed`, datasmall2))
    names(datasmall2)[1] <- "issue distance sexed"
    datasmall2_sexed <- datasmall2
    datasmall2_sexed$`issue distance sexed` <- as.factor(datasmall2_sexed$`issue distance sexed`)
    
    datasmall2 <- as.data.frame(cbind(data$`issue distance sexed`, datasmall2))
    names(datasmall2)[1] <- "issue distance sexed"
    datasmall2_sexed <- datasmall2
    datasmall2_sexed$`issue distance sexed` <- as.factor(datasmall2_sexed$`issue distance sexed`)
    
    results.rvint.sexed.noinds <- amce(chosen ~ `issue distance sexed` : `Grasp Issues` + 
                                         `issue distance sexed` : Persuasiveness + 
                                         `issue distance sexed` : `Professional Background` +
                                         `issue distance sexed` : `Dark Money` + 
                                         `issue distance sexed` : `Small Donors` + 
                                         `issue distance sexed` : `Total amount raised` +
                                         `issue distance sexed` : `Police Immigrant` + 
                                         `issue distance sexed` : `Sex Ed` + 
                                         `issue distance sexed` : `CF Pooled Compliance`, 
                                       data = datasmall2_sexed,  respondent.id = "ParticipantID", cluster = T, 
                                       design = design, baselines = baselines2, 
                                       respondent.varying = "`issue distance sexed`")
    
    plot(results.rvint.sexed.noinds, main = "", xlab = "Change in E[Y]",
         ci = 0.95, colors = NULL, 
         xlim = NULL, breaks = NULL,
         labels = NULL, attribute.names = NULL,
         level.names = NULL,
         label.baseline = F, text.size = 8, text.color = "black",
         point.size = 0.5, dodge.size = 0.9, plot.theme = NULL,
         plot.display = "interaction",
         facet.names = NULL, facet.levels = NULL,
         group.order = group.order2)
  
    
    ## Now kick out a table with these results for the appendix.
    ## each column is issue distance.

    amce.ID0.est <- round(summary(results.rvint.sexed.noinds)$issuedistancesexed1amce$Estimate, digits = 2)
    amce.ID0.SE.cilow <- round(summary(results.rvint.sexed.noinds)$issuedistancesexed1amce$Estimate, digits = 2) - 
      2*(summary(results.rvint.sexed.noinds)$issuedistancesexed1amce$`Std. Err`)
    amce.ID0.SE.cihigh <- round(summary(results.rvint.sexed.noinds)$issuedistancesexed1amce$Estimate, digits = 2) +
      2*(summary(results.rvint.sexed.noinds)$issuedistancesexed1amce$`Std. Err`)
    amce.ID0.CI.part1 <- paste("[", round(amce.ID0.SE.cilow, digits = 2), sep = "")
    amce.ID0.CI.part2 <- paste(round(amce.ID0.SE.cihigh, digits = 2), "]", sep = "")
    amce.ID0.CI <- paste(amce.ID0.CI.part1, amce.ID0.CI.part2, sep = ",")
    
    
    amce.ID1.est <- round(summary(results.rvint.sexed.noinds)$issuedistancesexed2amce$Estimate, digits = 2)
    amce.ID1.SE.cilow <- round(summary(results.rvint.sexed.noinds)$issuedistancesexed2amce$Estimate, digits = 2) - 
      2*(summary(results.rvint.sexed.noinds)$issuedistancesexed2amce$`Std. Err`)
    amce.ID1.SE.cihigh <- round(summary(results.rvint.sexed.noinds)$issuedistancesexed2amce$Estimate, digits = 2) +
      2*(summary(results.rvint.sexed.noinds)$issuedistancesexed2amce$`Std. Err`)
    amce.ID1.CI.part1 <- paste("[", round(amce.ID1.SE.cilow, digits = 2), sep = "")
    amce.ID1.CI.part2 <- paste(round(amce.ID1.SE.cihigh, digits = 2), "]", sep = "")
    amce.ID1.CI <- paste(amce.ID1.CI.part1, amce.ID1.CI.part2, sep = ",")
    
    
    amce.ID2.est <- round(summary(results.rvint.sexed.noinds)$issuedistancesexed3amce$Estimate, digits = 2)
    amce.ID2.SE.cilow <- round(summary(results.rvint.sexed.noinds)$issuedistancesexed3amce$Estimate, digits = 2) - 
      2*(summary(results.rvint.sexed.noinds)$issuedistancesexed3amce$`Std. Err`)
    amce.ID2.SE.cihigh <- round(summary(results.rvint.sexed.noinds)$issuedistancesexed3amce$Estimate, digits = 2) +
      2*(summary(results.rvint.sexed.noinds)$issuedistancesexed3amce$`Std. Err`)
    amce.ID2.CI.part1 <- paste("[", round(amce.ID2.SE.cilow, digits = 2), sep = "")
    amce.ID2.CI.part2 <- paste(round(amce.ID2.SE.cihigh, digits = 2), "]", sep = "")
    amce.ID2.CI <- paste(amce.ID2.CI.part1, amce.ID2.CI.part2, sep = ",")
    
    
    amce.ID3.est <- round(summary(results.rvint.sexed.noinds)$issuedistancesexed4amce$Estimate, digits = 2)
    amce.ID3.SE.cilow <- round(summary(results.rvint.sexed.noinds)$issuedistancesexed4amce$Estimate, digits = 2) - 
      2*(summary(results.rvint.sexed.noinds)$issuedistancesexed4amce$`Std. Err`)
    amce.ID3.SE.cihigh <- round(summary(results.rvint.sexed.noinds)$issuedistancesexed4amce$Estimate, digits = 2) +
      2*(summary(results.rvint.sexed.noinds)$issuedistancesexed4amce$`Std. Err`)
    amce.ID3.CI.part1 <- paste("[", round(amce.ID3.SE.cilow, digits = 2), sep = "")
    amce.ID3.CI.part2 <- paste(round(amce.ID3.SE.cihigh, digits = 2), "]", sep = "")
    amce.ID3.CI <- paste(amce.ID3.CI.part1, amce.ID3.CI.part2, sep = ",")
    
    amce.sexed.attributes.noinds <- summary(results.rvint.sexed.noinds)$issuedistancesexed1amce$Level
    
    sexed.issuedist.table.noinds <- as.data.frame(cbind(amce.sexed.attributes.noinds, amce.ID0.est, amce.ID0.CI, 
                                                        amce.ID1.est, amce.ID1.CI, 
                                                        amce.ID2.est, amce.ID2.CI, 
                                                        amce.ID3.est, amce.ID3.CI))
    
    # using sexed.issuedist.table to create the figure
    
    fig6new.noinds <- sexed.issuedist.table.noinds[sexed.issuedist.table.noinds$amce.sexed.attributes == 
                                                     "overcomplied" |
                                                     sexed.issuedist.table.noinds$amce.sexed.attributes == 
                                                     "noncompliant" |
                                                     sexed.issuedist.table.noinds$amce.sexed.attributes == 
                                                     "discouraged_dark_money"|
                                                     sexed.issuedist.table.noinds$amce.sexed.attributes == 
                                                     "raised_dark_money",]
    
    sexed.issuedist.table.cis <- subset(fig6new.noinds, select = c(amce.sexed.attributes.noinds,
                                                                   amce.ID0.CI, amce.ID1.CI,
                                                                   amce.ID2.CI,amce.ID3.CI))
    # get ci high and ci low
    # ID0
    amce.ID0.CI <- sexed.issuedist.table.cis$amce.ID0.CI
    amce.ID0.CI <- gsub("\\[|\\]", "", amce.ID0.CI)
    ID0.cilow <- substr(amce.ID0.CI, 1, 5)
    ID0.cilow <- gsub("\\,", "", ID0.cilow)
    ID0.cihigh <- substr(amce.ID0.CI, 6, 10)
    ID0.cihigh <- gsub("\\,", "", ID0.cihigh)
    
    # ID1
    amce.ID1.CI <- sexed.issuedist.table.cis$amce.ID1.CI
    amce.ID1.CI <- gsub("\\[|\\]", "", amce.ID1.CI)
    ID1.cilow <- substr(amce.ID1.CI, 1, 5)
    ID1.cilow <- gsub("\\,", "", ID1.cilow)
    ID1.cihigh <- substr(amce.ID1.CI, 6, 10)
    ID1.cihigh <- gsub("\\,", "", ID1.cihigh)
    ID1.cihigh[3] <- "-0.07"
    
    # ID2
    amce.ID2.CI <- sexed.issuedist.table.cis$amce.ID2.CI
    amce.ID2.CI <- gsub("\\[|\\]", "", amce.ID2.CI)
    ID2.cilow <- substr(amce.ID2.CI, 1, 5)
    ID2.cilow <- gsub("\\,", "", ID2.cilow)
    ID2.cihigh <- substr(amce.ID2.CI, 6, 10)
    ID2.cihigh <- gsub("\\,", "", ID2.cihigh)
    
    # ID3
    amce.ID3.CI <- sexed.issuedist.table.cis$amce.ID3.CI
    amce.ID3.CI <- gsub("\\[|\\]", "", amce.ID3.CI)
    ID3.cilow <- substr(amce.ID3.CI, 1, 5)
    ID3.cilow <- gsub("\\,", "", ID3.cilow)
    ID3.cihigh <- substr(amce.ID3.CI, 6, 10)
    ID3.cihigh <- gsub("\\,", "", ID3.cihigh)
    
    # save to merge with reshaped data 
    amce.cihigh <- cbind(ID0.cihigh, ID1.cihigh, ID2.cihigh, ID3.cihigh)
    amce.cilow <- cbind(ID0.cilow, ID1.cilow, ID2.cilow, ID3.cilow)
    sexed.issuedist.table.new <- cbind(fig6new.noinds, amce.cihigh, amce.cilow)
    
    # get rid of the pasted CIs now
    sexed.issuedist.table.new <- subset(sexed.issuedist.table.new, select = -c(amce.ID0.CI, amce.ID1.CI, 
                                                                               amce.ID2.CI, amce.ID3.CI))
    # reshape code was breaking so changing it to a data table.
    sedt.noinds <- as.data.table(sexed.issuedist.table.new)
    
    f6long <- reshape2::melt(sedt.noinds, id = 'amce.sexed.attributes.noinds', value.name = "value")
    
    # create index vectors to sort by
    f6long$order_atts <- rep(c(3, 4, 1, 2), times = 12)
    f6long$order_IDs <- rep(c(rep(1, times = 4), rep(2, times = 4), rep(3, times = 4),
                              rep(4, times = 4)), times = 3)
    
    f6long <- f6long[order(f6long$order_atts, f6long$order_IDs),]
    
    f6long$`Policy Distance` <- rep(c(rep("0", times = 3), rep("1", times = 3),
                                      rep("2", times = 3), rep("3", times = 3)), times = 4)
    
    f6long$stat <- rep(c("estimate", "ci.high", "ci.low"), times = 16)
    
    f6longtmp <- subset(f6long, select = c(amce.sexed.attributes.noinds, value, `Policy Distance`, stat))
    f6wide <- dcast(f6longtmp, amce.sexed.attributes.noinds + `Policy Distance` ~ stat, value.var = "value")
    
    # now just re-sort f6wide to get it to go from ext cons to ext lib again and in attribute order.
    f6wide$distanceorder <- rep(c(4, 3, 2, 1), times = 4)
    f6wide$attributeorder <- c(rep(2, times = 4), rep(4, times = 4), rep(3, times = 4), rep(1, times = 4))
    f6wide <- f6wide[order(f6wide$attributeorder, f6wide$distanceorder),]
    f6wide$reorder <- index(1:length(f6wide$attributeorder))
    f6wide$addtoy <- c(0, 0, 0, 0, .3, .3, .3, .3, .7, .7, .7, .7, .9, .9, .9, .9)
    f6wide$ci.low <- as.numeric(f6wide$ci.low)
    f6wide$ci.high <- as.numeric(f6wide$ci.high)
    f6wide$pch <- rep(c(18, 17, 16, 15), times = 4)
    
    
    ### Figure A6B ----
    
    # plot initialization
    par(mar=c(5, 1, 4, 2))
    plot(0,0 , xlim=c(-0.3, 0.25) , ylim=c(0,5.5), col="white" , yaxt="n" , ylab="", 
         xlab="", xaxt = "n")
    
    # fill the graph
    for (i in 1:length(f6wide$estimate)){
      points(x=f6wide$estimate[i], y=((f6wide$reorder[i]*0.1)+(f6wide$reorder[i]/8) + f6wide$addtoy[i]), 
             pch = f6wide$pch[i])
      
      segments(x0 = f6wide$ci.low[i], y0 = ((f6wide$reorder[i]*0.1)+(f6wide$reorder[i]/8)+ f6wide$addtoy[i]), 
               x1 = f6wide$ci.high[i], y1 = ((f6wide$reorder[i]*0.1)+(f6wide$reorder[i]/8)+ f6wide$addtoy[i]))
    }
    #  add vertical line at 0
    segments(x0 = 0, x1 = 0, y0 = 0, y1 = 5.5, lty = 2, col = "black")
    
    # add / format axes
    #axis(2, at = c(0.5, 2, 3.5, 4.9), labels = unique(f6wide$amce.sexed.attributes), 
    #     tick = TRUE, col = "black", las = 1, cex.axis = 0.8)
    axis(1, at = c(-0.2, -0.1, 0.0, 0.1, 0.2), tick = TRUE, 
         cex.axis = 0.8)
    
    legend("topright", legend = c("0", "1", "2", "3"), 
           pch = c(15, 16, 17, 18), 
           title = "|Policy Distance|", cex = 0.5, ncol = 1)
    
    mtext("Change in E[Y]", side = 1, line=2, cex = 0.7, at = c(0,0))
    

  ## FIGURES 6, A6B, A7A, and TABLE A7B, immigration start here ----
  data$supimmig <- NA
  data$supimmig[data$SupImmigPolicy == "Strongly Support"] <- 4
  data$supimmig[data$SupImmigPolicy == "Support"] <- 3
  data$supimmig[data$SupImmigPolicy == "Oppose"] <- 2
  data$supimmig[data$SupImmigPolicy == "Strongly Oppose"] <- 1
  
  data$csupimmig <- NA
  data$csupimmig[data$`Police Immigrant` == "Strongly Supports"] <- 4
  data$csupimmig[data$`Police Immigrant` == "Supports"] <- 3
  data$csupimmig[data$`Police Immigrant` == "Opposes"] <- 2
  data$csupimmig[data$`Police Immigrant` == "Strongly opposes"] <- 1
  
  data$`issue distance immig` <- abs(data$supimmig - data$csupimmig)
  
  datasmall <- as.data.frame(cbind(data$`issue distance immig`, datasmall))
  names(datasmall)[1] <- "issue distance immig"
  datasmall_immig <- datasmall
  
  datasmall_immig$`issue distance immig` <- as.factor(datasmall_immig$`issue distance immig`)
  
  results.rvint.immig <- amce(chosen ~ `issue distance immig` : `Grasp Issues` + 
                                `issue distance immig` : Persuasiveness + 
                                `issue distance immig` : `Professional Background` +
                                `issue distance immig` : `Dark Money` + 
                                `issue distance immig` : `Small Donors` + 
                                `issue distance immig` : `Total amount raised` +
                                `issue distance immig` : `Police Immigrant` + 
                                `issue distance immig` : `Sex Ed` + 
                                `issue distance immig` : `CF Pooled Compliance`, 
                              data = datasmall_immig,  respondent.id = "ParticipantID", cluster = T, 
                              design = design, baselines = baselines2, 
                              respondent.varying = "`issue distance immig`")
  
  ## APPENDIX Figure A7A, immigration policy ----
  plot(results.rvint.immig, main = "", xlab = "Change in E[Y]",
       ci = 0.95, colors = NULL, 
       xlim = NULL, breaks = NULL,
       labels = NULL, attribute.names = NULL,
       level.names = NULL,
       label.baseline = F, text.size = 8, text.color = "black",
       point.size = 0.5, dodge.size = 0.9, plot.theme = NULL,
       plot.display = "interaction",
       facet.names = NULL, facet.levels = NULL,
       group.order = group.order2)


## Table A7B, immigration columns ----
## each column is issue distance.
  amce.ID0.est <- round(summary(results.rvint.immig)$issuedistanceimmig1amce$Estimate, digits = 2)
  amce.ID0.SE.cilow <- round(summary(results.rvint.immig)$issuedistanceimmig1amce$Estimate, digits = 2) - 
    2*(summary(results.rvint.immig)$issuedistanceimmig1amce$`Std. Err`)
  amce.ID0.SE.cihigh <- round(summary(results.rvint.immig)$issuedistanceimmig1amce$Estimate, digits = 2) +
    2*(summary(results.rvint.immig)$issuedistanceimmig1amce$`Std. Err`)
  amce.ID0.CI.part1 <- paste("[", round(amce.ID0.SE.cilow, digits = 2), sep = "")
  amce.ID0.CI.part2 <- paste(round(amce.ID0.SE.cihigh, digits = 2), "]", sep = "")
  amce.ID0.CI <- paste(amce.ID0.CI.part1, amce.ID0.CI.part2, sep = ",")
  
  
  amce.ID1.est <- round(summary(results.rvint.immig)$issuedistanceimmig2amce$Estimate, digits = 2)
  amce.ID1.SE.cilow <- round(summary(results.rvint.immig)$issuedistanceimmig2amce$Estimate, digits = 2) - 
    2*(summary(results.rvint.immig)$issuedistanceimmig2amce$`Std. Err`)
  amce.ID1.SE.cihigh <- round(summary(results.rvint.immig)$issuedistanceimmig2amce$Estimate, digits = 2) +
    2*(summary(results.rvint.immig)$issuedistanceimmig2amce$`Std. Err`)
  amce.ID1.CI.part1 <- paste("[", round(amce.ID1.SE.cilow, digits = 2), sep = "")
  amce.ID1.CI.part2 <- paste(round(amce.ID1.SE.cihigh, digits = 2), "]", sep = "")
  amce.ID1.CI <- paste(amce.ID1.CI.part1, amce.ID1.CI.part2, sep = ",")
  
  
  amce.ID2.est <- round(summary(results.rvint.immig)$issuedistanceimmig3amce$Estimate, digits = 2)
  amce.ID2.SE.cilow <- round(summary(results.rvint.immig)$issuedistanceimmig3amce$Estimate, digits = 2) - 
    2*(summary(results.rvint.immig)$issuedistanceimmig3amce$`Std. Err`)
  amce.ID2.SE.cihigh <- round(summary(results.rvint.immig)$issuedistanceimmig3amce$Estimate, digits = 2) +
    2*(summary(results.rvint.immig)$issuedistanceimmig3amce$`Std. Err`)
  amce.ID2.CI.part1 <- paste("[", round(amce.ID2.SE.cilow, digits = 2), sep = "")
  amce.ID2.CI.part2 <- paste(round(amce.ID2.SE.cihigh, digits = 2), "]", sep = "")
  amce.ID2.CI <- paste(amce.ID2.CI.part1, amce.ID2.CI.part2, sep = ",")
  
  
  amce.ID3.est <- round(summary(results.rvint.immig)$issuedistanceimmig4amce$Estimate, digits = 2)
  amce.ID3.SE.cilow <- round(summary(results.rvint.immig)$issuedistanceimmig4amce$Estimate, digits = 2) - 
    2*(summary(results.rvint.immig)$issuedistanceimmig4amce$`Std. Err`)
  amce.ID3.SE.cihigh <- round(summary(results.rvint.immig)$issuedistanceimmig4amce$Estimate, digits = 2) +
    2*(summary(results.rvint.immig)$issuedistanceimmig4amce$`Std. Err`)
  amce.ID3.CI.part1 <- paste("[", round(amce.ID3.SE.cilow, digits = 2), sep = "")
  amce.ID3.CI.part2 <- paste(round(amce.ID3.SE.cihigh, digits = 2), "]", sep = "")
  amce.ID3.CI <- paste(amce.ID3.CI.part1, amce.ID3.CI.part2, sep = ",")
  
  amce.immig.attributes <- summary(results.rvint.immig)$issuedistanceimmig1amce$Level

  ### All immigration-related table results for Table A7B ----
  immig.issuedist.table <- as.data.frame(cbind(amce.immig.attributes, amce.ID0.est, amce.ID0.CI, 
                                               amce.ID1.est, amce.ID1.CI, 
                                               amce.ID2.est, amce.ID2.CI, 
                                               amce.ID3.est, amce.ID3.CI))

# Setting up the code for Figure 6, immigration panel
  fig6new <- immig.issuedist.table[immig.issuedist.table$amce.immig.attributes == "overcomplied" |
                                     immig.issuedist.table$amce.immig.attributes == "noncompliant" |
                                     immig.issuedist.table$amce.immig.attributes == "discouraged_dark_money"|
                                     immig.issuedist.table$amce.immig.attributes == "raised_dark_money",]
  
  immig.issuedist.table.cis <- subset(fig6new, select = c(amce.immig.attributes,
                                                          amce.ID0.CI, amce.ID1.CI,
                                                          amce.ID2.CI,amce.ID3.CI))
  # get ci high and ci low
  # ID0
  amce.ID0.CI <- immig.issuedist.table.cis$amce.ID0.CI
  amce.ID0.CI <- gsub("\\[|\\]", "", amce.ID0.CI)
  ID0.cilow <- substr(amce.ID0.CI, 1, 5)
  ID0.cilow <- gsub("\\,", "", ID0.cilow)
  ID0.cihigh <- substr(amce.ID0.CI, 6, 10)
  ID0.cihigh <- gsub("\\,", "", ID0.cihigh)
  ID0.cihigh[3] <- "-0.08"
  
  
  # ID1
  amce.ID1.CI <- immig.issuedist.table.cis$amce.ID1.CI
  amce.ID1.CI <- gsub("\\[|\\]", "", amce.ID1.CI)
  ID1.cilow <- substr(amce.ID1.CI, 1, 5)
  ID1.cilow <- gsub("\\,", "", ID1.cilow)
  ID1.cihigh <- substr(amce.ID1.CI, 6, 10)
  ID1.cihigh <- gsub("\\,", "", ID1.cihigh)
  
  
  # ID2
  amce.ID2.CI <- immig.issuedist.table.cis$amce.ID2.CI
  amce.ID2.CI <- gsub("\\[|\\]", "", amce.ID2.CI)
  ID2.cilow <- substr(amce.ID2.CI, 1, 5)
  ID2.cilow <- gsub("\\,", "", ID2.cilow)
  ID2.cihigh <- substr(amce.ID2.CI, 6, 10)
  ID2.cihigh <- gsub("\\,", "", ID2.cihigh)
  ID2.cilow[4] <- "0"
  ID2.cihigh[4] <- "0.08"
  ID2.cilow[3] <- "-0.15"
  ID2.cihigh[3] <- "-0.07"
  
  # ID3
  amce.ID3.CI <- immig.issuedist.table.cis$amce.ID3.CI
  amce.ID3.CI <- gsub("\\[|\\]", "", amce.ID3.CI)
  ID3.cilow <- substr(amce.ID3.CI, 1, 5)
  ID3.cilow <- gsub("\\,", "", ID3.cilow)
  ID3.cihigh <- substr(amce.ID3.CI, 6, 10)
  ID3.cihigh <- gsub("\\,", "", ID3.cihigh)
  
  # save to merge with reshaped data 
  amce.cihigh <- cbind(ID0.cihigh, ID1.cihigh, ID2.cihigh, ID3.cihigh)
  amce.cilow <- cbind(ID0.cilow, ID1.cilow, ID2.cilow, ID3.cilow)
  immig.issuedist.table.new <- cbind(fig6new, amce.cihigh, amce.cilow)
  
  # get rid of the pasted CIs now
  immig.issuedist.table.new <- subset(immig.issuedist.table.new, select = -c(amce.ID0.CI, amce.ID1.CI, 
                                                                             amce.ID2.CI, amce.ID3.CI))
  
  f6long <- reshape2::melt(immig.issuedist.table.new, id = 'amce.immig.attributes', value.name = "value")
  
  # create index vectors to sort by
  f6long$order_atts <- rep(c(3, 4, 1, 2), times = 12)
  f6long$order_IDs <- rep(c(rep(1, times = 4), rep(2, times = 4), rep(3, times = 4),
                            rep(4, times = 4)), times = 3)
  
  f6long <- f6long[order(f6long$order_atts, f6long$order_IDs),]
  
  f6long$`Policy Distance` <- rep(c(rep("0", times = 3), rep("1", times = 3),
                                    rep("2", times = 3), rep("3", times = 3)), times = 4)
  
  f6long$stat <- rep(c("estimate", "ci.high", "ci.low"), times = 16)
  
  f6longtmp <- subset(f6long, select = c(amce.immig.attributes, value, `Policy Distance`, stat))
  f6wide <- dcast(f6longtmp, amce.immig.attributes + `Policy Distance` ~ stat, value.var = "value")
  
  # now just re-sort f6wide to get it to go from ext cons to ext lib again and in attribute order.
  f6wide$distanceorder <- rep(c(4, 3, 2, 1), times = 4)
  f6wide$attributeorder <- c(rep(2, times = 4), rep(4, times = 4), rep(3, times = 4), rep(1, times = 4))
  f6wide <- f6wide[order(f6wide$attributeorder, f6wide$distanceorder),]
  f6wide$reorder <- index(1:length(f6wide$attributeorder))
  f6wide$addtoy <- c(0, 0, 0, 0, .3, .3, .3, .3, .7, .7, .7, .7, .9, .9, .9, .9)
  f6wide$ci.low <- as.numeric(f6wide$ci.low)
  f6wide$ci.high <- as.numeric(f6wide$ci.high)
  f6wide$pch <- rep(c(18, 17, 16, 15), times = 4)

## FIGURE 6, immigration panel ----
# plot initialization
    par(mar=c(5, 10, 4, 2))
    plot(0,0 , xlim=c(-0.3, 0.25) , ylim=c(0,4.5), col="white" , yaxt="n" , ylab="", 
         xlab="", xaxt = "n")
    
    # fill the graph
    for (i in 1:length(f6wide$estimate)){
      points(x=f6wide$estimate[i], y=((f6wide$reorder[i]*0.1)+(f6wide$reorder[i]/8) + f6wide$addtoy[i]), 
             pch = f6wide$pch[i])
      
      segments(x0 = f6wide$ci.low[i], y0 = ((f6wide$reorder[i]*0.1)+(f6wide$reorder[i]/8)+ f6wide$addtoy[i]), 
               x1 = f6wide$ci.high[i], y1 = ((f6wide$reorder[i]*0.1)+(f6wide$reorder[i]/8)+ f6wide$addtoy[i]))
    }
    #  add vertical line at 0
    segments(x0 = 0, x1 = 0, y0 = 0, y1 = 4.5, lty = 2, col = "black")
    
    # add / format axes
    axis(2, at = c(.6, 1.8, 3.1, 4.3), labels = unique(f6wide$amce.immig.attributes), 
         tick = TRUE, col = "black", las = 1, cex.axis = 0.8)
    axis(1, at = c(-0.2, -0.1, 0.0, 0.1, 0.2), tick = TRUE, 
         cex.axis = 0.8)
    
    mtext("Change in E[Y]", side = 1, line=2, cex = 0.7, at = c(0,0))




# claim in text:  "For example, respondents ranged between -0.10 [-0.13, -0.07] 
    # and -0.14 [-0.18, -0.10] in their AMCEs on noncompliance.  
    # Their reward to candidates who discouraged dark money was also fairly stable, 
    # ranging between 0.04 [0.00, 0.08] and 0.07 [0.04, 0.10]. 
    # The same is true for other non-policy traits, 
    # such as grasp of issues, funding from small donors, and total amount raised 
    # (Appendix 7)."
    
    # see relevant variables (noncompliant, discouraged dark money, grasp of issues, 
    # funding from small donors, total amount raised)
immig.issuedist.table
sexed.issuedist.table


## FIG A6B immigration plot without independents #####
# respondent immig support
datasmall2$supimmig <- NA
datasmall2$supimmig[datasmall2$SupImmigPolicy == "Strongly Support"] <- 4
datasmall2$supimmig[datasmall2$SupImmigPolicy == "Support"] <- 3
datasmall2$supimmig[datasmall2$SupImmigPolicy == "Oppose"] <- 2
datasmall2$supimmig[datasmall2$SupImmigPolicy == "Strongly Oppose"] <- 1

# candidate immig support
datasmall2$csupimmig <- NA
datasmall2$csupimmig[datasmall2$`Police Immigrant` == "Strongly Supports"] <- 4
datasmall2$csupimmig[datasmall2$`Police Immigrant` == "Supports"] <- 3
datasmall2$csupimmig[datasmall2$`Police Immigrant` == "Opposes"] <- 2
datasmall2$csupimmig[datasmall2$`Police Immigrant` == "Strongly opposes"] <- 1

datasmall2$`issue distance immig` <- abs(datasmall2$supimmig - datasmall2$csupimmig)


datasmall2 <- as.data.frame(cbind(datasmall2$`issue distance immig`, datasmall2))
names(datasmall2)[1] <- "issue distance immig"
datasmall2_immig <- datasmall2
datasmall2_immig$`issue distance immig` <- as.factor(datasmall2_immig$`issue distance immig`)

results.rvint.immig.noinds <- amce(chosen ~ `issue distance immig` : `Grasp Issues` + 
                                     `issue distance immig` : Persuasiveness + 
                                     `issue distance immig` : `Professional Background` +
                                     `issue distance immig` : `Dark Money` + 
                                     `issue distance immig` : `Small Donors` + 
                                     `issue distance immig` : `Total amount raised` +
                                     `issue distance immig` : `Police Immigrant` + 
                                     `issue distance immig` : `Sex Ed` + 
                                     `issue distance immig` : `CF Pooled Compliance`, 
                                   data = datasmall2_immig,  respondent.id = "ParticipantID", cluster = T, 
                                   design = design, baselines = baselines2, 
                                   respondent.varying = "`issue distance immig`")



plot(results.rvint.immig.noinds, main = "", xlab = "Change in E[Y]",
     ci = 0.95, colors = NULL, 
     xlim = NULL, breaks = NULL,
     labels = NULL, attribute.names = NULL,
     level.names = NULL,
     label.baseline = F, text.size = 8, text.color = "black",
     point.size = 0.5, dodge.size = 0.9, plot.theme = NULL,
     plot.display = "interaction",
     facet.names = NULL, facet.levels = NULL,
     group.order = group.order2)

## Now kick out a table with these results for the appendix.
## each column is issue distance.

amce.ID0.est <- round(summary(results.rvint.immig.noinds)$issuedistanceimmig1amce$Estimate, digits = 2)
amce.ID0.SE.cilow <- round(summary(results.rvint.immig.noinds)$issuedistanceimmig1amce$Estimate, digits = 2) - 
  2*(summary(results.rvint.immig.noinds)$issuedistanceimmig1amce$`Std. Err`)
amce.ID0.SE.cihigh <- round(summary(results.rvint.immig.noinds)$issuedistanceimmig1amce$Estimate, digits = 2) +
  2*(summary(results.rvint.immig.noinds)$issuedistanceimmig1amce$`Std. Err`)
amce.ID0.CI.part1 <- paste("[", round(amce.ID0.SE.cilow, digits = 2), sep = "")
amce.ID0.CI.part2 <- paste(round(amce.ID0.SE.cihigh, digits = 2), "]", sep = "")
amce.ID0.CI <- paste(amce.ID0.CI.part1, amce.ID0.CI.part2, sep = ",")


amce.ID1.est <- round(summary(results.rvint.immig.noinds)$issuedistanceimmig2amce$Estimate, digits = 2)
amce.ID1.SE.cilow <- round(summary(results.rvint.immig.noinds)$issuedistanceimmig2amce$Estimate, digits = 2) - 
  2*(summary(results.rvint.immig.noinds)$issuedistanceimmig2amce$`Std. Err`)
amce.ID1.SE.cihigh <- round(summary(results.rvint.immig.noinds)$issuedistanceimmig2amce$Estimate, digits = 2) +
  2*(summary(results.rvint.immig.noinds)$issuedistanceimmig2amce$`Std. Err`)
amce.ID1.CI.part1 <- paste("[", round(amce.ID1.SE.cilow, digits = 2), sep = "")
amce.ID1.CI.part2 <- paste(round(amce.ID1.SE.cihigh, digits = 2), "]", sep = "")
amce.ID1.CI <- paste(amce.ID1.CI.part1, amce.ID1.CI.part2, sep = ",")


amce.ID2.est <- round(summary(results.rvint.immig.noinds)$issuedistanceimmig3amce$Estimate, digits = 2)
amce.ID2.SE.cilow <- round(summary(results.rvint.immig.noinds)$issuedistanceimmig3amce$Estimate, digits = 2) - 
  2*(summary(results.rvint.immig.noinds)$issuedistanceimmig3amce$`Std. Err`)
amce.ID2.SE.cihigh <- round(summary(results.rvint.immig.noinds)$issuedistanceimmig3amce$Estimate, digits = 2) +
  2*(summary(results.rvint.immig.noinds)$issuedistanceimmig3amce$`Std. Err`)
amce.ID2.CI.part1 <- paste("[", round(amce.ID2.SE.cilow, digits = 2), sep = "")
amce.ID2.CI.part2 <- paste(round(amce.ID2.SE.cihigh, digits = 2), "]", sep = "")
amce.ID2.CI <- paste(amce.ID2.CI.part1, amce.ID2.CI.part2, sep = ",")


amce.ID3.est <- round(summary(results.rvint.immig.noinds)$issuedistanceimmig4amce$Estimate, digits = 2)
amce.ID3.SE.cilow <- round(summary(results.rvint.immig.noinds)$issuedistanceimmig4amce$Estimate, digits = 2) - 
  2*(summary(results.rvint.immig.noinds)$issuedistanceimmig4amce$`Std. Err`)
amce.ID3.SE.cihigh <- round(summary(results.rvint.immig.noinds)$issuedistanceimmig4amce$Estimate, digits = 2) +
  2*(summary(results.rvint.immig.noinds)$issuedistanceimmig4amce$`Std. Err`)
amce.ID3.CI.part1 <- paste("[", round(amce.ID3.SE.cilow, digits = 2), sep = "")
amce.ID3.CI.part2 <- paste(round(amce.ID3.SE.cihigh, digits = 2), "]", sep = "")
amce.ID3.CI <- paste(amce.ID3.CI.part1, amce.ID3.CI.part2, sep = ",")

amce.immig.attributes.noinds <- summary(results.rvint.immig.noinds)$issuedistanceimmig1amce$Level

immig.issuedist.table.noinds <- as.data.frame(cbind(amce.immig.attributes.noinds, amce.ID0.est, amce.ID0.CI, 
                                                    amce.ID1.est, amce.ID1.CI, 
                                                    amce.ID2.est, amce.ID2.CI, 
                                                    amce.ID3.est, amce.ID3.CI))

# using immig.issuedist.table to create the figure

fig6new.noinds <- immig.issuedist.table.noinds[immig.issuedist.table.noinds$amce.immig.attributes == 
                                                 "overcomplied" |
                                                 immig.issuedist.table.noinds$amce.immig.attributes == 
                                                 "noncompliant" |
                                                 immig.issuedist.table.noinds$amce.immig.attributes == 
                                                 "discouraged_dark_money"|
                                                 immig.issuedist.table.noinds$amce.immig.attributes == 
                                                 "raised_dark_money",]

immig.issuedist.table.cis <- subset(fig6new.noinds, select = c(amce.immig.attributes.noinds,
                                                               amce.ID0.CI, amce.ID1.CI,
                                                               amce.ID2.CI,amce.ID3.CI))
# get ci high and ci low
# ID0
amce.ID0.CI <- immig.issuedist.table.cis$amce.ID0.CI
amce.ID0.CI <- gsub("\\[|\\]", "", amce.ID0.CI)
ID0.cilow <- substr(amce.ID0.CI, 1, 5)
ID0.cilow <- gsub("\\,", "", ID0.cilow)
ID0.cihigh <- substr(amce.ID0.CI, 6, 10)
ID0.cihigh <- gsub("\\,", "", ID0.cihigh)
ID0.cihigh[3] <- "-0.08"

# ID1
amce.ID1.CI <- immig.issuedist.table.cis$amce.ID1.CI
amce.ID1.CI <- gsub("\\[|\\]", "", amce.ID1.CI)
ID1.cilow <- substr(amce.ID1.CI, 1, 5)
ID1.cilow <- gsub("\\,", "", ID1.cilow)
ID1.cihigh <- substr(amce.ID1.CI, 6, 10)
ID1.cihigh <- gsub("\\,", "", ID1.cihigh)
ID1.cihigh[3] <- "-0.11"

# ID2
amce.ID2.CI <- immig.issuedist.table.cis$amce.ID2.CI
amce.ID2.CI <- gsub("\\[|\\]", "", amce.ID2.CI)
ID2.cilow <- substr(amce.ID2.CI, 1, 5)
ID2.cilow <- gsub("\\,", "", ID2.cilow)
ID2.cihigh <- substr(amce.ID2.CI, 6, 10)
ID2.cihigh <- gsub("\\,", "", ID2.cihigh)
ID2.cihigh[3] <- "-0.06"

# ID3
amce.ID3.CI <- immig.issuedist.table.cis$amce.ID3.CI
amce.ID3.CI <- gsub("\\[|\\]", "", amce.ID3.CI)
ID3.cilow <- substr(amce.ID3.CI, 1, 5)
ID3.cilow <- gsub("\\,", "", ID3.cilow)
ID3.cihigh <- substr(amce.ID3.CI, 6, 10)
ID3.cihigh <- gsub("\\,", "", ID3.cihigh)


# save to merge with reshaped data 
amce.cihigh <- cbind(ID0.cihigh, ID1.cihigh, ID2.cihigh, ID3.cihigh)
amce.cilow <- cbind(ID0.cilow, ID1.cilow, ID2.cilow, ID3.cilow)
immig.issuedist.table.new <- cbind(fig6new.noinds, amce.cihigh, amce.cilow)

# get rid of the pasted CIs now
immig.issuedist.table.new <- subset(immig.issuedist.table.new, select = -c(amce.ID0.CI, amce.ID1.CI, 
                                                                           amce.ID2.CI, amce.ID3.CI))
# reshape code was breaking so changing it to a data table.
immdt.noinds <- as.data.table(immig.issuedist.table.new)
f6long <- reshape2::melt(immdt.noinds, id = 'amce.immig.attributes.noinds', value.name = "value")

# create index vectors to sort by
f6long$order_atts <- rep(c(3, 4, 1, 2), times = 12)
f6long$order_IDs <- rep(c(rep(1, times = 4), rep(2, times = 4), rep(3, times = 4),
                          rep(4, times = 4)), times = 3)

f6long <- f6long[order(f6long$order_atts, f6long$order_IDs),]

f6long$`Policy Distance` <- rep(c(rep("0", times = 3), rep("1", times = 3),
                                  rep("2", times = 3), rep("3", times = 3)), times = 4)

f6long$stat <- rep(c("estimate", "ci.high", "ci.low"), times = 16)

f6longtmp <- subset(f6long, select = c(amce.immig.attributes.noinds, value, `Policy Distance`, stat))
f6wide <- reshape2::dcast(f6longtmp, amce.immig.attributes.noinds + `Policy Distance` ~ stat, value.var = "value")

# now just re-sort f6wide to get it to go from ext cons to ext lib again and in attribute order.
f6wide$distanceorder <- rep(c(4, 3, 2, 1), times = 4)
f6wide$attributeorder <- c(rep(2, times = 4), rep(4, times = 4), rep(3, times = 4), rep(1, times = 4))
f6wide <- f6wide[order(f6wide$attributeorder, f6wide$distanceorder),]
f6wide$reorder <- index(1:length(f6wide$attributeorder))
f6wide$addtoy <- c(0, 0, 0, 0, .3, .3, .3, .3, .7, .7, .7, .7, .9, .9, .9, .9)
f6wide$ci.low <- as.numeric(f6wide$ci.low)
f6wide$ci.high <- as.numeric(f6wide$ci.high)
f6wide$pch <- rep(c(18, 17, 16, 15), times = 4)

#### plot fig 6 for immig

# plot initialization
par(mar=c(5, 10, 4, 2))
plot(0,0 , xlim=c(-0.3, 0.25) , ylim=c(0,5.5), col="white" , yaxt="n" , ylab="", 
     xlab="", xaxt = "n")

# fill the graph
for (i in 1:length(f6wide$estimate)){
  points(x=f6wide$estimate[i], y=((f6wide$reorder[i]*0.1)+(f6wide$reorder[i]/8) + f6wide$addtoy[i]), 
         pch = f6wide$pch[i])
  
  segments(x0 = f6wide$ci.low[i], y0 = ((f6wide$reorder[i]*0.1)+(f6wide$reorder[i]/8)+ f6wide$addtoy[i]), 
           x1 = f6wide$ci.high[i], y1 = ((f6wide$reorder[i]*0.1)+(f6wide$reorder[i]/8)+ f6wide$addtoy[i]))
}
#  add vertical line at 0
segments(x0 = 0, x1 = 0, y0 = 0, y1 = 5.5, lty = 2, col = "black")

# add / format axes
axis(2, at = c(0.5, 2, 3.5, 4.9), labels = unique(f6wide$amce.immig.attributes), 
     tick = TRUE, col = "black", las = 1, cex.axis = 0.8)
axis(1, at = c(-0.2, -0.1, 0.0, 0.1, 0.2), tick = TRUE, 
     cex.axis = 0.8)

mtext("Change in E[Y]", side = 1, line=2, cex = 0.7, at = c(0,0))





# FIGURE 7, APPENDIX 8A-C, A6C, analysis by party ID/assignment ----

# Expectation:  If transparency is a valence issue, then Y | p < 4 and Y | p > 4 
# will have same reaction. 

# Analyze R and D together 
datasmall <- cbind(data$PID, datasmall)
names(datasmall)[1] <- "PID"
datasmallparty <- subset(datasmall, !(is.na(PID)))
datasmallparty$PID <- as.numeric(as.character(datasmallparty$PID))

datasmallparty$Repubs <- as.numeric(datasmallparty$PID < 4)
datasmallparty$Dems <- as.numeric(datasmallparty$PID > 4)

datasmallparty$partyID <- NA
datasmallparty$partyID[datasmallparty$Repubs == 1] <- "Republican"
datasmallparty$partyID[datasmallparty$Dems == 1] <- "Democrats"

datasmallparty <- subset(datasmallparty, !(is.na(datasmallparty$partyID)))
datasmallparty$partyID <- as.factor(datasmallparty$partyID)


## Republicans analysis ----
reps <-  subset(datasmall, Party == "R")

rattribute_list <- list()
rattribute_list[["Party"]] <- c("D", "R")  
rattribute_list[["Police Immigrant"]] <- c("Opposes", "Strongly opposes",  "Supports", "Strongly Supports")
rattribute_list[["`Sex Ed`"]] <-  c("Opposes", "Strongly opposes",  "Supports", "Strongly Supports")
rattribute_list[["CF Compliance"]] <- c("in_compliance", "missed_deadlines", "reporting_gaps", 
                                        "donors_on_site", "map_on_site")
rattribute_list[["Dark Money"]] <- 
  c("dark_money_support", "discouraged_dark_money", "raised_dark_money")
rattribute_list[["Total amount raised"]] <- c("$250,000", "$500,000", "$750,000")
rattribute_list[["Small Donors"]] <- c("40%", "60%", "80%")
rattribute_list[["Professional Background"]] <- c("Student", "Head of State Agency", 
                                                  "Mayor", "Small Business Owner", 
                                                  "School Principal")
rattribute_list[["Persuasiveness"]] <- 
  c("Somewhat persuasive", "Not very persuasive",  "Extremely persuasive")
rattribute_list[["Grasp Issues"]] <- 
  c("Good grasp", "Weak grasp",  "Excellent grasp")
rattribute_list[["CF Pooled Compliance"]] <- c("complied", "overcomplied", "noncompliant")


rdesign <- makeDesign(type = "constraints", attribute.levels = rattribute_list)

rresults <- amce(chosen ~ `CF Pooled Compliance` + `Grasp Issues` + Persuasiveness + 
                   `Professional Background` +
                   `Dark Money` + `Small Donors` + `Total amount raised` +
                   `Police Immigrant` + `Sex Ed`, data = reps, 
                 respondent.id = "ParticipantID", cluster = TRUE, design=rdesign, 
                 baselines = baselines2)

## Democrats analysis ----

dems <-  subset(datasmall, Party == "D")

dattribute_list <- list()
dattribute_list[["Party"]] <- c("D", "R")  
dattribute_list[["Police Immigrant"]] <- c("Opposes", "Strongly opposes",  "Supports", "Strongly Supports")
dattribute_list[["`Sex Ed`"]] <-  c("Opposes", "Strongly opposes",  "Supports", "Strongly Supports")
dattribute_list[["CF Compliance"]] <- c("in_compliance", "missed_deadlines", "reporting_gaps", 
                                        "donors_on_site", "map_on_site")
dattribute_list[["Dark Money"]] <- 
  c("dark_money_support", "discouraged_dark_money", "raised_dark_money")
dattribute_list[["Total amount raised"]] <- c("$250,000", "$500,000", "$750,000")
dattribute_list[["Small Donors"]] <- c("40%", "60%", "80%")
dattribute_list[["Professional Background"]] <- c("Student", "Head of State Agency", 
                                                  "Mayor", "Small Business Owner", 
                                                  "School Principal")
dattribute_list[["Persuasiveness"]] <- 
  c("Somewhat persuasive", "Not very persuasive",  "Extremely persuasive")
dattribute_list[["Grasp Issues"]] <- 
  c("Good grasp", "Weak grasp",  "Excellent grasp")
dattribute_list[["CF Pooled Compliance"]] <- c("complied", "overcomplied", "noncompliant")


ddesign <- makeDesign(type = "constraints", attribute.levels = dattribute_list)

dresults <- amce(chosen ~ `CF Pooled Compliance` + `Grasp Issues` + Persuasiveness + 
                   `Professional Background` +
                   `Dark Money` + `Small Donors` + `Total amount raised` +
                   `Police Immigrant` + `Sex Ed`, data = dems, 
                 respondent.id = "ParticipantID", cluster = TRUE, design=ddesign, 
                 baselines = baselines2)



## APPENDIX 8 FIGURE A8A, DEMOCRATS ----
plot(dresults, main = "", xlab = "Change in E[Y]",
     ci = 0.95, colors = NULL, 
     xlim = NULL, breaks = NULL,
     labels = NULL, attribute.names = NULL,
     level.names = NULL,
     label.baseline = T, text.size = 8, text.color = "black",
     point.size = 0.5, dodge.size = 0.9, plot.theme = NULL,
     plot.display = "all",
     facet.names = NULL, facet.levels = NULL,
     group.order = group.order2)

## APPENDIX 8 FIGURE A8A, REPUBLICANS ----
plot(rresults, main = "", xlab = "Change in E[Y]",
     ci = 0.95, colors = NULL, 
     xlim = c(-0.2, 0.2), breaks = NULL,
     labels = NULL, attribute.names = NULL,
     level.names = NULL,
     label.baseline = T, text.size = 8, text.color = "black",
     point.size = 0.5, dodge.size = 0.9, plot.theme = NULL,
     plot.display = "all",
     facet.names = NULL, facet.levels = NULL,
     group.order = group.order2)



## Republicans' AMCEs for Appendix 8 Table A8B----
amce.reps.est <- round(summary(rresults)$amce$Estimate, digits = 2)
amce.reps.SE.cilow <- round(summary(rresults)$amce$Estimate, digits = 2) - 
  2*(summary(rresults)$amce$`Std. Err`)
amce.reps.SE.cihigh <- round(summary(rresults)$amce$Estimate, digits = 2) +
  2*(summary(rresults)$amce$`Std. Err`)
amce.reps.CI.part1 <- paste("[", round(amce.reps.SE.cilow, digits = 2), sep = "")
amce.reps.CI.part2 <- paste(round(amce.reps.SE.cihigh, digits = 2), "]", sep = "")
amce.reps.CI <- paste(amce.reps.CI.part1, amce.reps.CI.part2, sep = ",")
amce.reps.level <- summary(rresults)$amce$Level

reps.table <- as.data.frame(cbind(amce.reps.level, amce.reps.est, amce.reps.CI))


## Democrats' AMCEs for Appendix 8 Table A8B----
amce.dems.est <- round(summary(dresults)$amce$Estimate, digits = 2)
amce.dems.SE.cilow <- round(summary(dresults)$amce$Estimate, digits = 2) - 
  2*(summary(dresults)$amce$`Std. Err`)
amce.dems.SE.cihigh <- round(summary(dresults)$amce$Estimate, digits = 2) +
  2*(summary(dresults)$amce$`Std. Err`)
amce.dems.CI.part1 <- paste("[", round(amce.dems.SE.cilow, digits = 2), sep = "")
amce.dems.CI.part2 <- paste(round(amce.dems.SE.cihigh, digits = 2), "]", sep = "")
amce.dems.CI <- paste(amce.dems.CI.part1, amce.dems.CI.part2, sep = ",")
amce.dems.level <- summary(dresults)$amce$Level

dems.table <- as.data.frame(cbind(amce.dems.level, amce.dems.est, amce.dems.CI))

## Appendix Table A8B ----
estimates.by.party.table <- as.data.frame(cbind(reps.table, dems.table))


## Figure 7 (manuscript) ----
# constructing matrix to make a plot by hand
# these are row names
levels <- c('Strong opp. pol-immig', 
            'Supp. pol-immig.', 
            'Strong supp. pol-immig.', 
            'Strong opp. sex ed', 
            'Supp. sex ed', 
            'Strong supp. sex ed', 
            "noncompliant", "overcomplied", "discouraged_dark_money", "raised_dark_money")

# These are the columns
party <- c("R", "D")

# vector of estimates
# each of these will be a row
# PI strong opp, PI supp, PI strong supp, 
# SE strong opp, SE supp, SE strong supp, 
# noncompliant, overcompliant; 
# dm_raised_prior, requested_no_dm
estimates_d <- c(NA, summary(dresults)$amce$Estimate[10], summary(dresults)$amce$Estimate[9],
                 NA, summary(dresults)$amce$Estimate[16], summary(dresults)$amce$Estimate[15],
                 summary(dresults)$amce$Estimate[1], summary(dresults)$amce$Estimate[2],
                 summary(dresults)$amce$Estimate[3], summary(dresults)$amce$Estimate[4])


estimates_r <- c(summary(rresults)$amce$Estimate[9], summary(rresults)$amce$Estimate[10], NA,
                 summary(rresults)$amce$Estimate[15], summary(rresults)$amce$Estimate[16], NA,
                 summary(rresults)$amce$Estimate[1], summary(rresults)$amce$Estimate[2],
                 summary(rresults)$amce$Estimate[3], summary(rresults)$amce$Estimate[4])

est <- c(estimates_d, estimates_r)

# vector of CIlow
SE_d <- c(NA, summary(dresults)$amce$`Std. Err`[10], summary(dresults)$amce$`Std. Err`[9],
          NA, summary(dresults)$amce$`Std. Err`[16], summary(dresults)$amce$`Std. Err`[15],
          summary(dresults)$amce$`Std. Err`[1], summary(dresults)$amce$`Std. Err`[2],
          summary(dresults)$amce$`Std. Err`[3], summary(dresults)$amce$`Std. Err`[4])


SE_r <- c(summary(rresults)$amce$`Std. Err`[9], summary(rresults)$amce$`Std. Err`[10], NA,
          summary(rresults)$amce$`Std. Err`[15], summary(rresults)$amce$`Std. Err`[16], NA,
          summary(rresults)$amce$`Std. Err`[1], summary(rresults)$amce$`Std. Err`[2],
          summary(rresults)$amce$`Std. Err`[3], summary(rresults)$amce$`Std. Err`[4])

# CIs
CIlow_d <- estimates_d - 1.96*SE_d
CIlow_r <- estimates_r - 1.96*SE_r
ci.low <- c(CIlow_d, CIlow_r)

CIhigh_d <- estimates_d + 1.96*SE_d
CIhigh_r <- estimates_r + 1.96*SE_r
ci.high <- c(CIhigh_d, CIhigh_r)

# put it all in a figure
level <- rep(levels, times = 2)
Party <- NA
Party[1:10] <- 'D'
Party[11:20] <- 'R'

fig7 <- as.data.frame(cbind(as.numeric(est), 
                            as.numeric(ci.low), 
                            as.numeric(ci.high), 
                            level,
                            Party), 
                      stringsAsFactors = F)
names(fig7) <- c('est', 'ci.low', 'ci.high', 'level', 'Party')

fig7$index <- c(1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

fig7 <- fig7[order(fig7$index, decreasing = T),]
fig7$plotorder <- c(1, 2, 3, 4, 7, 8, 9, 10, NA, 13, 14, 15, 16, NA, NA, 19, 20, 21, 22, NA)
labelshere <- c(1.5, 4, 7.5, 10, 13, 14.5, 16, 19, 20.5, 22)


fig7$est <- as.numeric(fig7$est)
fig7$ci.low <- as.numeric(fig7$ci.low)
fig7$ci.high <- as.numeric(fig7$ci.high)

fig7$pch[fig7$Party == "R"] <- 16
fig7$pch[fig7$Party == "D"] <- 17


# now plotting Figure 7

par(mar=c(5, 11, 4, 1)+.1)

plot(0,0 , xlim=c(-0.175, 0.16) , ylim=c(0,25), col="white" , yaxt="n" , ylab="", 
     xlab="", xaxt = "n")
#fill the graph
for (i in 1:length(fig7$est)){
  points(x=fig7$est[i], y=(fig7$plotorder[i]), pch = fig7$pch[i])
  segments(x0 = fig7$ci.low[i], y0 = (fig7$plotorder[i]), 
           x1 = fig7$ci.high[i], y1 = (fig7$plotorder[i]))
}
# add vertical line at 0
segments(x0 = 0, x1 = 0, y0 = 0, y1 = 25, lty = 2, col = "black")

# add gray horizontal (and dashed) lines between groups of variables
segments(x0 = -0.175, x1 = 0.16, y0 = 5.5, y1 = 5.5, lty = 1, col = "black")
segments(x0 = -0.175, x1 = 0.16, y0 = 11.5, y1 = 11.5, lty = 1, col = "black")
segments(x0 = -0.175, x1 = 0.16, y0 = 17.5, y1 = 17.5, lty = 1, col = "black")

# add gray dotted lines between policy positions for R5 -- hand-coding these based on labelshere, above
# labelshere <- c(1.5, 4, 7.5, 10, 13, 14.5, 16, 19, 20.5, 22)
# pol immig first
segments(x0 = -0.175, x1 = 0.16, y0 = 21.5, y1 = 21.5, lty = 1, col = "gray90")
segments(x0 = -0.175, x1 = 0.16, y0 = 19.5, y1 = 19.5, lty = 1, col = "gray90")

# sex ed
segments(x0 = -0.175, x1 = 0.16, y0 = 15.5, y1 = 15.5, lty = 1, col = "gray90")
segments(x0 = -0.175, x1 = 0.16, y0 = 13.5, y1 = 13.5, lty = 1, col = "gray90")

#after noncompliant
segments(x0 = -0.175, x1 = 0.16, y0 = 8.5, y1 = 8.5, lty = 1, col = "gray90")

# after discouraged_dark_money
segments(x0 = -0.175, x1 = 0.16, y0 = 2.5, y1 = 2.5, lty = 1, col = "gray90")

#add / format axes
axis(2, at = labelshere, labels = unique(fig7$level), 
     tick = TRUE, col = "black", las = 1, cex.axis = 1)
axis(1, at = c(-0.2, -0.1, 0.0, 0.1, 0.2), tick = TRUE, 
     cex.axis = 1)

# add legend
legend(x=0.075, y=25 , legend = c("Republicans", "Democrats"), pch = c(16, 17), 
       cex = 1)

mtext("Change in E[Y]", side = 1, line=2, cex = 1, at = c(0,0))


## Claim in text: Republicans’ AMCE is -0.09 [-0.12,-0.06] and Democrats’ is -0.14 [-0.17,-0.11]. ----
estimates.by.party.table[2,]

## Claim in text: Democrats are more rewarding of a candidate distancing herself from dark money (0.08 [0.05,0.09]), ----
# but Republicans do still reward that behavior (0.05 [0.02,0.08]; R-D marginal means difference -0.02 [-0.05, 0.00]). 


## Table in Appendix 8c to back up Fig 7----

# report out the diffs in marginal means
# first re-label factors, because mm_diffs doesn't like the repeated labels
datasmallparty$`Police Immigrant` <- revalue(datasmallparty$`Sex Ed`, c("Opposes" = "Opposes_imm", 
                                                                        "Strongly opposes" = "Strongly opposes_imm", 
                                                                        "Strongly Supports" = "Strongly Supports_imm", 
                                                                        "Supports" = "Supports_imm"))

datasmallparty$`Sex Ed` <- revalue(datasmallparty$`Sex Ed`, c("Opposes" = "Opposes_sex_ed", 
                                                              "Strongly opposes" = "Strongly_opposes_sex_ed", 
                                                              "Strongly Supports" = "Strongly_Supports_sex_ed", 
                                                              "Supports" = "Supports_sex_ed"))

mm_diffs_party <- mm_diffs(datasmallparty,
                           chosen ~ `CF Pooled Compliance` + `Grasp Issues` + Persuasiveness + 
                             `Professional Background` +
                             `Dark Money` + `Small Donors` + `Total amount raised` +
                             `Police Immigrant` + `Sex Ed`,
                           #by = ~Political_Party,
                           by = ~partyID,
                           id = ~ParticipantID, 
                           cluster = TRUE)

mm_diffs_party$estimate <- round(mm_diffs_party$estimate, digits = 2)
mm_diffs_party$std.error <- round(mm_diffs_party$std.error, digits = 2)
mm_diffs_party$z <- round(mm_diffs_party$z, digits = 2)
mm_diffs_party$p <- round(mm_diffs_party$p, digits = 2)
mm_diffs_party$ci_low <- round(mm_diffs_party$lower, digits = 2)
mm_diffs_party$ci_high <- round(mm_diffs_party$upper, digits = 2)

mm_diffs_party

## Figure A6C ----

### Republicans with no independents analysis ----
no_ind_reps <-  subset(datasmall, Party == "R" & randomized_independents == 0)

rresults_noind <- amce(chosen ~ `CF Pooled Compliance` + `Grasp Issues` + Persuasiveness + 
                   `Professional Background` +
                   `Dark Money` + `Small Donors` + `Total amount raised` +
                   `Police Immigrant` + `Sex Ed`, data = no_ind_reps, 
                 respondent.id = "ParticipantID", cluster = TRUE, design=rdesign, 
                 baselines = baselines2)

### Democrats with no independents analysis ----

no_ind_dems <-  subset(datasmall, Party == "D" & randomized_independents == 0)

dresults_noind <- amce(chosen ~ `CF Pooled Compliance` + `Grasp Issues` + Persuasiveness + 
                   `Professional Background` +
                   `Dark Money` + `Small Donors` + `Total amount raised` +
                   `Police Immigrant` + `Sex Ed`, data = no_ind_dems, 
                 respondent.id = "ParticipantID", cluster = TRUE, design=ddesign, 
                 baselines = baselines2)

# vector of estimates
# each of these will be a row
# PI strong opp, PI supp, PI strong supp, 
# SE strong opp, SE supp, SE strong supp, 
# noncompliant, overcompliant; 
# dm_raised_prior, requested_no_dm
estimates_d_noind <- c(NA, summary(dresults_noind)$amce$Estimate[10], summary(dresults_noind)$amce$Estimate[9],
                 NA, summary(dresults_noind)$amce$Estimate[16], summary(dresults_noind)$amce$Estimate[15],
                 summary(dresults_noind)$amce$Estimate[1], summary(dresults_noind)$amce$Estimate[2],
                 summary(dresults_noind)$amce$Estimate[3], summary(dresults_noind)$amce$Estimate[4])


estimates_r_noind <- c(summary(rresults_noind)$amce$Estimate[9], summary(rresults_noind)$amce$Estimate[10], NA,
                 summary(rresults_noind)$amce$Estimate[15], summary(rresults_noind)$amce$Estimate[16], NA,
                 summary(rresults_noind)$amce$Estimate[1], summary(rresults_noind)$amce$Estimate[2],
                 summary(rresults_noind)$amce$Estimate[3], summary(rresults_noind)$amce$Estimate[4])

est_noind <- c(estimates_d_noind, estimates_r_noind)

# vector of CIlow
SE_d_noind <- c(NA, summary(dresults_noind)$amce$`Std. Err`[10], summary(dresults_noind)$amce$`Std. Err`[9],
          NA, summary(dresults_noind)$amce$`Std. Err`[16], summary(dresults_noind)$amce$`Std. Err`[15],
          summary(dresults_noind)$amce$`Std. Err`[1], summary(dresults_noind)$amce$`Std. Err`[2],
          summary(dresults_noind)$amce$`Std. Err`[3], summary(dresults_noind)$amce$`Std. Err`[4])


SE_r_noind <- c(summary(rresults_noind)$amce$`Std. Err`[9], summary(rresults_noind)$amce$`Std. Err`[10], NA,
          summary(rresults_noind)$amce$`Std. Err`[15], summary(rresults_noind)$amce$`Std. Err`[16], NA,
          summary(rresults_noind)$amce$`Std. Err`[1], summary(rresults_noind)$amce$`Std. Err`[2],
          summary(rresults_noind)$amce$`Std. Err`[3], summary(rresults_noind)$amce$`Std. Err`[4])

# CIs
CIlow_d_noind <- estimates_d_noind - 1.96*SE_d_noind
CIlow_r_noind <- estimates_r_noind - 1.96*SE_r_noind
ci.low_noind <- c(CIlow_d_noind, CIlow_r_noind)

CIhigh_d_noind <- estimates_d_noind + 1.96*SE_d_noind
CIhigh_r_noind <- estimates_r_noind + 1.96*SE_r_noind
ci.high_noind <- c(CIhigh_d_noind, CIhigh_r_noind)

# put it all in a figure

figA6C <- as.data.frame(cbind(as.numeric(est_noind), 
                            as.numeric(ci.low_noind), 
                            as.numeric(ci.high_noind), 
                            level,
                            Party), 
                      stringsAsFactors = F)
names(figA6C) <- c('est', 'ci.low', 'ci.high', 'level', 'Party')

figA6C$index <- c(1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

figA6C <- figA6C[order(figA6C$index, decreasing = T),]
figA6C$plotorder <- c(1, 2, 3, 4, 7, 8, 9, 10, NA, 13, 14, 15, 16, NA, NA, 19, 20, 21, 22, NA)
labelshere <- c(1.5, 4, 7.5, 10, 13, 14.5, 16, 19, 20.5, 22)


figA6C$est <- as.numeric(figA6C$est)
figA6C$ci.low <- as.numeric(figA6C$ci.low)
figA6C$ci.high <- as.numeric(figA6C$ci.high)

figA6C$pch[fig7$Party == "R"] <- 16
figA6C$pch[fig7$Party == "D"] <- 17


# now plotting Figure A6C

par(mar=c(5, 11, 4, 1)+.1)

plot(0,0 , xlim=c(-0.175, 0.16) , ylim=c(0,25), col="white" , yaxt="n" , ylab="", 
     xlab="", xaxt = "n")
#fill the graph
for (i in 1:length(figA6C$est)){
  points(x=figA6C$est[i], y=(figA6C$plotorder[i]), pch = figA6C$pch[i])
  segments(x0 = figA6C$ci.low[i], y0 = (figA6C$plotorder[i]), 
           x1 = figA6C$ci.high[i], y1 = (figA6C$plotorder[i]))
}
# add vertical line at 0
segments(x0 = 0, x1 = 0, y0 = 0, y1 = 25, lty = 2, col = "black")

# add gray horizontal (and dashed) lines between groups of variables
segments(x0 = -0.175, x1 = 0.16, y0 = 5.5, y1 = 5.5, lty = 1, col = "black")
segments(x0 = -0.175, x1 = 0.16, y0 = 11.5, y1 = 11.5, lty = 1, col = "black")
segments(x0 = -0.175, x1 = 0.16, y0 = 17.5, y1 = 17.5, lty = 1, col = "black")

# add gray dotted lines between policy positions for R5 -- hand-coding these based on labelshere, above
# labelshere <- c(1.5, 4, 7.5, 10, 13, 14.5, 16, 19, 20.5, 22)
# pol immig first
segments(x0 = -0.175, x1 = 0.16, y0 = 21.5, y1 = 21.5, lty = 1, col = "gray90")
segments(x0 = -0.175, x1 = 0.16, y0 = 19.5, y1 = 19.5, lty = 1, col = "gray90")

# sex ed
segments(x0 = -0.175, x1 = 0.16, y0 = 15.5, y1 = 15.5, lty = 1, col = "gray90")
segments(x0 = -0.175, x1 = 0.16, y0 = 13.5, y1 = 13.5, lty = 1, col = "gray90")

#after noncompliant
segments(x0 = -0.175, x1 = 0.16, y0 = 8.5, y1 = 8.5, lty = 1, col = "gray90")

# after discouraged_dark_money
segments(x0 = -0.175, x1 = 0.16, y0 = 2.5, y1 = 2.5, lty = 1, col = "gray90")

#add / format axes
axis(2, at = labelshere, labels = unique(fig7$level), 
     tick = TRUE, col = "black", las = 1, cex.axis = 1)
axis(1, at = c(-0.2, -0.1, 0.0, 0.1, 0.2), tick = TRUE, 
     cex.axis = 1)

# add legend
legend(x=0.075, y=25 , legend = c("Republicans", "Democrats"), pch = c(16, 17), 
       cex = 1)

mtext("Change in E[Y]", side = 1, line=2, cex = 1, at = c(0,0))



# FIGURE 8, FIGURE A6D, Tables A9A, A9B ----
          
          datasmall$ideoextreme <- NA
          datasmall$ideoextreme[datasmall$ideo7 == "Extremely Conservative"] <- "Extremely Conservative"
          datasmall$ideoextreme[datasmall$ideo7 == "Conservative" | 
                                  datasmall$ideo7 == "Slightly Conservative"] <- "(Slightly) Conservative"
          datasmall$ideoextreme[datasmall$ideo7 == "Moderate; Middle of the Road" | 
                                  datasmall$ideo7 == "Haven't thought much about this"] <- "Moderate or don't know"
          datasmall$ideoextreme[datasmall$ideo7 == "Liberal" | 
                                  datasmall$ideo7 == "Slightly Liberal"] <- "(Slightly) Liberal"
          datasmall$ideoextreme[datasmall$ideo7 == "Extremely Liberal"] <- "Extremely Liberal"
          datasmall$ideoextreme <- as.factor(datasmall$ideoextreme)
         
          d7 <- subset(datasmall, !(is.na(ideoextreme)))
          d7 <- datasmall 
          
          results.rvint.ideoextreme <- amce(chosen ~ `ideoextreme` : `Grasp Issues` + 
                                              `ideoextreme` : Persuasiveness + 
                                              `ideoextreme` : `Professional Background` +
                                              `ideoextreme` : `Dark Money` + 
                                              `ideoextreme` : `Small Donors` + 
                                              `ideoextreme` : `Total amount raised` +
                                              `ideoextreme` : `Police Immigrant` + 
                                              `ideoextreme` : `Sex Ed` + 
                                              `ideoextreme` : `CF Pooled Compliance` , 
                                            data = d7,  respondent.id = "ParticipantID", cluster = T, 
                                            design = design, baselines = baselines2, 
                                            respondent.varying = "ideoextreme")
          
          
          plot(results.rvint.ideoextreme, main = "", xlab = "Change in E[Y]",
               ci = 0.95, colors = NULL, 
               xlim = NULL, breaks = NULL,
               labels = NULL, attribute.names = NULL,
               level.names = NULL,
               label.baseline = TRUE, text.size = 8, text.color = "black",
               point.size = 0.5, dodge.size = 0.9, plot.theme = NULL,
               plot.display = "all",
               facet.names = NULL, facet.levels = NULL,
               group.order = group.order2)
          
          
          
          ## Appendix Table A9A (to support Figure 8) ----

          amce.slightcons.est <- round(summary(results.rvint.ideoextreme)$ideoextreme1amce$Estimate, digits = 2)
          amce.slightcons.SE.cilow <- round(summary(results.rvint.ideoextreme)$ideoextreme1amce$Estimate, digits = 2) - 
            2*(summary(results.rvint.ideoextreme)$ideoextreme1amce$`Std. Err`)
          amce.slightcons.SE.cihigh <- round(summary(results.rvint.ideoextreme)$ideoextreme1amce$Estimate, digits = 2) +
            2*(summary(results.rvint.ideoextreme)$ideoextreme1amce$`Std. Err`)
          amce.slightcons.CI.part1 <- paste("[", round(amce.slightcons.SE.cilow, digits = 2), sep = "")
          amce.slightcons.CI.part2 <- paste(round(amce.slightcons.SE.cihigh, digits = 2), "]", sep = "")
          amce.slightcons.CI <- paste(amce.slightcons.CI.part1, amce.slightcons.CI.part2, sep = ",")
          
          
          amce.slightlib.est <- round(summary(results.rvint.ideoextreme)$ideoextreme2amce$Estimate, digits = 2)
          amce.slightlib.SE.cilow <- round(summary(results.rvint.ideoextreme)$ideoextreme2amce$Estimate, digits = 2) - 
            2*(summary(results.rvint.ideoextreme)$ideoextreme2amce$`Std. Err`)
          amce.slightlib.SE.cihigh <- round(summary(results.rvint.ideoextreme)$ideoextreme2amce$Estimate, digits = 2) +
            2*(summary(results.rvint.ideoextreme)$ideoextreme2amce$`Std. Err`)
          amce.slightlib.CI.part1 <- paste("[", round(amce.slightlib.SE.cilow, digits = 2), sep = "")
          amce.slightlib.CI.part2 <- paste(round(amce.slightlib.SE.cihigh, digits = 2), "]", sep = "")
          amce.slightlib.CI <- paste(amce.slightlib.CI.part1, amce.slightlib.CI.part2, sep = ",")
          
          
          amce.extcons.est <- round(summary(results.rvint.ideoextreme)$ideoextreme3amce$Estimate, digits = 2)
          amce.extcons.SE.cilow <- round(summary(results.rvint.ideoextreme)$ideoextreme3amce$Estimate, digits = 2) - 
            2*(summary(results.rvint.ideoextreme)$ideoextreme3amce$`Std. Err`)
          amce.extcons.SE.cihigh <- round(summary(results.rvint.ideoextreme)$ideoextreme3amce$Estimate, digits = 2) +
            2*(summary(results.rvint.ideoextreme)$ideoextreme3amce$`Std. Err`)
          amce.extcons.CI.part1 <- paste("[", round(amce.extcons.SE.cilow, digits = 2), sep = "")
          amce.extcons.CI.part2 <- paste(round(amce.extcons.SE.cihigh, digits = 2), "]", sep = "")
          amce.extcons.CI <- paste(amce.extcons.CI.part1, amce.extcons.CI.part2, sep = ",")
          
          
          amce.extlib.est <- round(summary(results.rvint.ideoextreme)$ideoextreme4amce$Estimate, digits = 2)
          amce.extlib.SE.cilow <- round(summary(results.rvint.ideoextreme)$ideoextreme4amce$Estimate, digits = 2) - 
            2*(summary(results.rvint.ideoextreme)$ideoextreme4amce$`Std. Err`)
          amce.extlib.SE.cihigh <- round(summary(results.rvint.ideoextreme)$ideoextreme4amce$Estimate, digits = 2) +
            2*(summary(results.rvint.ideoextreme)$ideoextreme4amce$`Std. Err`)
          amce.extlib.CI.part1 <- paste("[", round(amce.extlib.SE.cilow, digits = 2), sep = "")
          amce.extlib.CI.part2 <- paste(round(amce.extlib.SE.cihigh, digits = 2), "]", sep = "")
          amce.extlib.CI <- paste(amce.extlib.CI.part1, amce.extlib.CI.part2, sep = ",")
          
          amce.moderate.est <- round(summary(results.rvint.ideoextreme)$ideoextreme5amce$Estimate, digits = 2)
          amce.moderate.SE.cilow <- round(summary(results.rvint.ideoextreme)$ideoextreme5amce$Estimate, digits = 2) - 
            2*(summary(results.rvint.ideoextreme)$ideoextreme5amce$`Std. Err`)
          amce.moderate.SE.cihigh <- round(summary(results.rvint.ideoextreme)$ideoextreme5amce$Estimate, digits = 2) +
            2*(summary(results.rvint.ideoextreme)$ideoextreme5amce$`Std. Err`)
          amce.moderate.CI.part1 <- paste("[", round(amce.moderate.SE.cilow, digits = 2), sep = "")
          amce.moderate.CI.part2 <- paste(round(amce.moderate.SE.cihigh, digits = 2), "]", sep = "")
          amce.moderate.CI <- paste(amce.moderate.CI.part1, amce.moderate.CI.part2, sep = ",")
          
          amce.ideo.attributes <- summary(results.rvint.ideoextreme)$ideoextreme1amce$Level
          
          ### Final Table A9A presented here as ideo.table / ideo.table.cis ----
          ideo.table <- as.data.frame(cbind(amce.ideo.attributes, amce.extcons.est, amce.extcons.CI, 
                                            amce.slightcons.est, amce.slightcons.CI, 
                                            amce.moderate.est, amce.moderate.CI, 
                                            amce.slightlib.est, amce.slightlib.CI, 
                                            amce.extlib.est, amce.extlib.CI))


          fig8new <- ideo.table[ideo.table$amce.ideo.attributes == "overcomplied" |
                                  ideo.table$amce.ideo.attributes == "noncompliant" |
                                  ideo.table$amce.ideo.attributes == "discouraged_dark_money"|
                                  ideo.table$amce.ideo.attributes == "raised_dark_money",]
          
          ideo.table.cis <- subset(fig8new, select = c(amce.ideo.attributes,
                                                       amce.extcons.CI, amce.slightcons.CI, 
                                                       amce.moderate.CI, amce.slightlib.CI, 
                                                       amce.extlib.CI))
          # get ci high and ci low
          # extcons
          amce.extcons.CI <- ideo.table.cis$amce.extcons.CI
          amce.extcons.CI <- gsub("\\[|\\]", "", amce.extcons.CI)
          extcon.cilow <- substr(amce.extcons.CI, 1, 5)
          extcon.cilow <- gsub("\\,", "", extcon.cilow)
          extcon.cihigh <- substr(amce.extcons.CI, 6, 10)
          extcon.cihigh <- gsub("\\,", "", extcon.cihigh)
          
          # slightcons
          amce.slightcons.CI <- ideo.table.cis$amce.slightcons.CI
          amce.slightcons.CI <- gsub("\\[|\\]", "", amce.slightcons.CI)
          slightcon.cilow <- substr(amce.slightcons.CI, 1, 5)
          slightcon.cilow <- gsub("\\,", "", slightcon.cilow)
          slightcon.cihigh <- substr(amce.slightcons.CI, 6, 11)
          slightcon.cihigh <- gsub("\\,", "", slightcon.cihigh)
          
          
          # moderate
          amce.moderate.CI <- ideo.table.cis$amce.moderate.CI
          amce.moderate.CI <- gsub("\\[|\\]", "", amce.moderate.CI)
          moderate.cilow <- substr(amce.moderate.CI, 1, 5)
          moderate.cilow <- gsub("\\,", "", moderate.cilow)
          moderate.cihigh <- substr(amce.moderate.CI, 6, 11)
          
          # slightlib
          amce.slightlib.CI <- ideo.table.cis$amce.slightlib.CI
          amce.slightlib.CI <- gsub("\\[|\\]", "", amce.slightlib.CI)
          slightlib.cilow <- substr(amce.slightlib.CI, 1, 5)
          slightlib.cilow <- gsub("\\,", "", slightlib.cilow)
          slightlib.cihigh <- substr(amce.slightlib.CI, 6, 11)
          slightlib.cihigh <- gsub("\\,", "", slightlib.cihigh)
          
          # extlib
          amce.extlib.CI <- ideo.table.cis$amce.extlib.CI
          amce.extlib.CI <- gsub("\\[|\\]", "", amce.extlib.CI)
          extlib.cilow <- substr(amce.extlib.CI, 1, 5)
          extlib.cilow <- gsub("\\,", "", extlib.cilow)
          extlib.cihigh <- substr(amce.extlib.CI, 6, 11)
          extlib.cihigh <- gsub("\\,", "", extlib.cihigh)
          
          # save to merge with reshaped data
          amce.cihigh <- cbind(extcon.cihigh, slightcon.cihigh, moderate.cihigh, 
                               slightlib.cihigh, extlib.cihigh)
          amce.cilow <- cbind(extcon.cilow, slightcon.cilow, moderate.cilow, 
                              slightlib.cilow, extlib.cilow)
          ideo.table.new <- cbind(fig8new, amce.cihigh, amce.cilow)
          
          # get rid of the pasted CIs now
          ideo.table.new <- subset(ideo.table.new, select = -c(amce.extcons.CI, amce.slightcons.CI, 
                                                               amce.moderate.CI, amce.slightlib.CI, 
                                                               amce.extlib.CI))
    
          f8long <- melt(as.data.table(ideo.table.new), id = 'amce.ideo.attributes', value.name = "value")
          
          # create index vectors to sort by
          f8long$order_atts <- rep(c(3, 4, 1, 2), times = 15)
          f8long$order_ideo <- rep(c(rep(5, times = 4), rep(4, times = 4), rep(3, times = 4),
                                     rep(2, times = 4), rep(1, times = 4)), times = 3)
          
          f8long <- f8long[order(f8long$order_atts, f8long$order_ideo),]
          
          f8long$ideology <- rep(c(rep("ext lib", times = 3), rep("slight lib", times = 3),
                                   rep("moderate", times = 3), rep("slight cons", times = 3),
                                   rep("ext cons", times = 3)), times = 4)
          
          f8long$stat <- rep(c("estimate", "ci.high", "ci.low"), times = 20)
          
          f8longtmp <- subset(f8long, select = c(amce.ideo.attributes, value, ideology, stat))
          f8wide <- dcast(f8longtmp, amce.ideo.attributes + ideology ~ stat, value.var = "value")
          
          # now just re-sort f8wide to get it to go from ext cons to ext lib again and in attribute order.
          f8wide$ideologyorder <- rep(c(1, 5, 3, 2, 4), times = 4)
          f8wide$attributeorder <- c(rep(2, times = 5), rep(4, times = 5), rep(3, times = 5), rep(1, times = 5))
          f8wide <- f8wide[order(f8wide$attributeorder, f8wide$ideologyorder),]
          f8wide$reorder <- index(1:length(f8wide$ideologyorder))
          f8wide$addtoy <- c(0, 0, 0, 0, 0, .3, .3, .3, .3, .3, .7, .7, .7, .7, .7, .9, .9, .9, .9, .9)
          f8wide$ci.low <- as.numeric(f8wide$ci.low)
          f8wide$ci.high <- as.numeric(gsub(",","",f8wide$ci.high))
          f8wide$pch <- rep(c(1, 18, 17, 16, 0), times = 4)
          
          ## Plot Fig 8 ----
         
          # plot initialization
          par(mar=c(5, 10, 4, 2))
          plot(0,0 , xlim=c(-0.3, 0.25) , ylim=c(0,5.5), col="white" , yaxt="n" , ylab="", 
               xlab="", xaxt = "n")
          
          # fill the graph
          for (i in 1:length(f8wide$estimate)){
            points(x=f8wide$estimate[i], y=((f8wide$reorder[i]*0.1)+(f8wide$reorder[i]/8) + f8wide$addtoy[i]), 
                   pch = f8wide$pch[i])
            
            segments(x0 = f8wide$ci.low[i], y0 = ((f8wide$reorder[i]*0.1)+(f8wide$reorder[i]/8)+ f8wide$addtoy[i]), 
                     x1 = f8wide$ci.high[i], y1 = ((f8wide$reorder[i]*0.1)+(f8wide$reorder[i]/8)+ f8wide$addtoy[i]))
          }
          #  add vertical line at 0
          segments(x0 = 0, x1 = 0, y0 = 0, y1 = 5.5, lty = 2, col = "black")
          
          # add / format axes
          axis(2, at = c(0.5, 2, 3.5, 4.9), labels = unique(f8wide$amce.ideo.attributes), 
               tick = TRUE, col = "black", las = 1, cex.axis = 0.8)
          axis(1, at = c(-0.2, -0.1, 0.0, 0.1, 0.2), tick = TRUE, 
               cex.axis = 0.8)
          
          legend("topright", legend = c("Extremely Liberal", "Liberal or Slightly Lib.", "Moderate or don't know",  
                                        "Conservative or Slightly Cons.", "Extremely Conservative"), 
                 pch = c(0, 16, 17, 18, 1), 
                 title = "Ideology", cex = 0.5, ncol = 1)
          
          mtext("Change in E[Y]", side = 1, line=2, cex = 0.7, at = c(0,0)) 
          
          
          ## Appendix Table A9B: marginal means table to support Fig 8 ----
          datasmall2ideo <- subset(datasmall2, !(is.na(ideo7)))
          datasmall2ideo$ext <- as.numeric(datasmall2ideo$ideo7 == "Extremely Liberal" | 
                                             datasmall2ideo$ideo7 == "Extremely Conservative")
          datasmall2ideo$extreme <- as.factor(datasmall2ideo$ext)
          
          # fix labels for Sex Ed so cregg() will run
          datasmall2ideo$`Sex Ed` <- revalue(datasmall2ideo$`Sex Ed`, c("Opposes" = "Opposes_sex_ed", 
                                                                        "Strongly opposes" = "Strongly_opposes_sex_ed", 
                                                                        "Strongly Supports" = "Strongly_Supports_sex_ed", 
                                                                        "Supports" = "Supports_sex_ed"))
          
          tmp_ideo <- cj(chosen ~ `CF Pooled Compliance` + `Grasp Issues` + Persuasiveness + 
                           `Professional Background` +
                           `Dark Money` + `Small Donors` + `Total amount raised` +
                           `Police Immigrant` + `Sex Ed`, data = datasmall2ideo, 
                         id = ~ParticipantID, estimate = "mm", by = ~ extreme, cluster = TRUE, 
                         design=design_cregg)
          
         
          plot(tmp_ideo, group = "extreme", vline = 0.5) +
            scale_colour_manual(labels = c("Extreme", "More Moderate"),
                                values = c("black", "gray"))
          
          mm_diffs_ideo <- mm_diffs(datasmall2ideo,
                                     chosen ~ `CF Pooled Compliance` + `Grasp Issues` + Persuasiveness + 
                                       `Professional Background` +
                                       `Dark Money` + `Small Donors` + `Total amount raised` +
                                       `Police Immigrant` + `Sex Ed`,
                                     by = ~extreme,
                                     id = ~ParticipantID, 
                                     cluster = TRUE)
           
           mm_diffs_ideo$estimate <- round(mm_diffs_ideo$estimate, digits = 2)
           mm_diffs_ideo$std.error <- round(mm_diffs_ideo$std.error, digits = 2)
           mm_diffs_ideo$z <- round(mm_diffs_ideo$z, digits = 2)
           mm_diffs_ideo$p <- round(mm_diffs_ideo$p, digits = 2)
           mm_diffs_ideo$ci_low <- round(mm_diffs_ideo$lower, digits = 2)
           mm_diffs_ideo$ci_high <- round(mm_diffs_ideo$upper, digits = 2)
          
          # here's the table (Appendix A9B)
           mm_diffs_ideo
           
          
          ## APPENDIX Figure A6D -----
          # replicating Figure 8 without independents
          
          amce.slightcons.est <- round(summary(results.rvint.ideoextreme)$ideoextreme1amce$Estimate, digits = 2)
          amce.slightcons.SE.cilow <- round(summary(results.rvint.ideoextreme)$ideoextreme1amce$Estimate, digits = 2) - 
            2*(summary(results.rvint.ideoextreme)$ideoextreme1amce$`Std. Err`)
          amce.slightcons.SE.cihigh <- round(summary(results.rvint.ideoextreme)$ideoextreme1amce$Estimate, digits = 2) +
            2*(summary(results.rvint.ideoextreme)$ideoextreme1amce$`Std. Err`)
          amce.slightcons.CI.part1 <- paste("[", round(amce.slightcons.SE.cilow, digits = 2), sep = "")
          amce.slightcons.CI.part2 <- paste(round(amce.slightcons.SE.cihigh, digits = 2), "]", sep = "")
          amce.slightcons.CI <- paste(amce.slightcons.CI.part1, amce.slightcons.CI.part2, sep = ",")
          
          
          amce.slightlib.est <- round(summary(results.rvint.ideoextreme)$ideoextreme2amce$Estimate, digits = 2)
          amce.slightlib.SE.cilow <- round(summary(results.rvint.ideoextreme)$ideoextreme2amce$Estimate, digits = 2) - 
            2*(summary(results.rvint.ideoextreme)$ideoextreme2amce$`Std. Err`)
          amce.slightlib.SE.cihigh <- round(summary(results.rvint.ideoextreme)$ideoextreme2amce$Estimate, digits = 2) +
            2*(summary(results.rvint.ideoextreme)$ideoextreme2amce$`Std. Err`)
          amce.slightlib.CI.part1 <- paste("[", round(amce.slightlib.SE.cilow, digits = 2), sep = "")
          amce.slightlib.CI.part2 <- paste(round(amce.slightlib.SE.cihigh, digits = 2), "]", sep = "")
          amce.slightlib.CI <- paste(amce.slightlib.CI.part1, amce.slightlib.CI.part2, sep = ",")
          
          
          amce.extcons.est <- round(summary(results.rvint.ideoextreme)$ideoextreme3amce$Estimate, digits = 2)
          amce.extcons.SE.cilow <- round(summary(results.rvint.ideoextreme)$ideoextreme3amce$Estimate, digits = 2) - 
            2*(summary(results.rvint.ideoextreme)$ideoextreme3amce$`Std. Err`)
          amce.extcons.SE.cihigh <- round(summary(results.rvint.ideoextreme)$ideoextreme3amce$Estimate, digits = 2) +
            2*(summary(results.rvint.ideoextreme)$ideoextreme3amce$`Std. Err`)
          amce.extcons.CI.part1 <- paste("[", round(amce.extcons.SE.cilow, digits = 2), sep = "")
          amce.extcons.CI.part2 <- paste(round(amce.extcons.SE.cihigh, digits = 2), "]", sep = "")
          amce.extcons.CI <- paste(amce.extcons.CI.part1, amce.extcons.CI.part2, sep = ",")
          
          
          amce.extlib.est <- round(summary(results.rvint.ideoextreme)$ideoextreme4amce$Estimate, digits = 2)
          amce.extlib.SE.cilow <- round(summary(results.rvint.ideoextreme)$ideoextreme4amce$Estimate, digits = 2) - 
            2*(summary(results.rvint.ideoextreme)$ideoextreme4amce$`Std. Err`)
          amce.extlib.SE.cihigh <- round(summary(results.rvint.ideoextreme)$ideoextreme4amce$Estimate, digits = 2) +
            2*(summary(results.rvint.ideoextreme)$ideoextreme4amce$`Std. Err`)
          amce.extlib.CI.part1 <- paste("[", round(amce.extlib.SE.cilow, digits = 2), sep = "")
          amce.extlib.CI.part2 <- paste(round(amce.extlib.SE.cihigh, digits = 2), "]", sep = "")
          amce.extlib.CI <- paste(amce.extlib.CI.part1, amce.extlib.CI.part2, sep = ",")
          
          amce.moderate.est <- round(summary(results.rvint.ideoextreme)$ideoextreme5amce$Estimate, digits = 2)
          amce.moderate.SE.cilow <- round(summary(results.rvint.ideoextreme)$ideoextreme5amce$Estimate, digits = 2) - 
            2*(summary(results.rvint.ideoextreme)$ideoextreme5amce$`Std. Err`)
          amce.moderate.SE.cihigh <- round(summary(results.rvint.ideoextreme)$ideoextreme5amce$Estimate, digits = 2) +
            2*(summary(results.rvint.ideoextreme)$ideoextreme5amce$`Std. Err`)
          amce.moderate.CI.part1 <- paste("[", round(amce.moderate.SE.cilow, digits = 2), sep = "")
          amce.moderate.CI.part2 <- paste(round(amce.moderate.SE.cihigh, digits = 2), "]", sep = "")
          amce.moderate.CI <- paste(amce.moderate.CI.part1, amce.moderate.CI.part2, sep = ",")
          
          amce.ideo.attributes <- summary(results.rvint.ideoextreme)$ideoextreme1amce$Level
          
          ideo.table.ni <- as.data.frame(cbind(amce.ideo.attributes, amce.extcons.est, amce.extcons.CI, 
                                            amce.slightcons.est, amce.slightcons.CI, 
                                            amce.moderate.est, amce.moderate.CI, 
                                            amce.slightlib.est, amce.slightlib.CI, 
                                            amce.extlib.est, amce.extlib.CI))
          
          figA6D <- ideo.table.ni[ideo.table.ni$amce.ideo.attributes == "overcomplied" |
                                    ideo.table.ni$amce.ideo.attributes == "noncompliant" |
                                    ideo.table.ni$amce.ideo.attributes == "discouraged_dark_money"|
                                    ideo.table.ni$amce.ideo.attributes == "raised_dark_money",]
          
          ideo.table.ni.cis <- subset(figA6D, select = c(amce.ideo.attributes,
                                                         amce.extcons.CI, amce.slightcons.CI, 
                                                         amce.moderate.CI, amce.slightlib.CI, 
                                                         amce.extlib.CI))
          # get ci high and ci low
          # extcons
          amce.extcons.CI <- ideo.table.ni.cis$amce.extcons.CI
          amce.extcons.CI <- gsub("\\[|\\]", "", amce.extcons.CI)
          extcon.cilow <- substr(amce.extcons.CI, 1, 5)
          extcon.cilow <- gsub("\\,", "", extcon.cilow)
          extcon.cihigh <- substr(amce.extcons.CI, 6, 10)
          extcon.cihigh <- gsub("\\,", "", extcon.cihigh)
          
          # slightcons
          amce.slightcons.CI <- ideo.table.ni.cis$amce.slightcons.CI
          amce.slightcons.CI <- gsub("\\[|\\]", "", amce.slightcons.CI)
          slightcon.cilow <- substr(amce.slightcons.CI, 1, 5)
          slightcon.cilow <- gsub("\\,", "", slightcon.cilow)
          slightcon.cihigh <- substr(amce.slightcons.CI, 6, 11)
          slightcon.cihigh <- gsub("\\,", "", slightcon.cihigh)
          
          
          # moderate
          amce.moderate.CI <- ideo.table.ni.cis$amce.moderate.CI
          amce.moderate.CI <- gsub("\\[|\\]", "", amce.moderate.CI)
          moderate.cilow <- substr(amce.moderate.CI, 1, 5)
          moderate.cilow <- gsub("\\,", "", moderate.cilow)
          moderate.cihigh <- substr(amce.moderate.CI, 6, 11)
          
          # fix by hand for no-independents version - happened bc substr is so rigid, 
          # so replugging the ideo.table.ni.cis values here.
          moderate.cihigh[[2]] <- "0.05"
          moderate.cihigh[[3]] <- "-0.07"
          moderate.cihigh[[4]] <- "0.1"
          moderate.cilow[[4]] <- "0"
          
          # slightlib
          amce.slightlib.CI <- ideo.table.ni.cis$amce.slightlib.CI
          amce.slightlib.CI <- gsub("\\[|\\]", "", amce.slightlib.CI)
          slightlib.cilow <- substr(amce.slightlib.CI, 1, 5)
          slightlib.cilow <- gsub("\\,", "", slightlib.cilow)
          slightlib.cihigh <- substr(amce.slightlib.CI, 6, 11)
          slightlib.cihigh <- gsub("\\,", "", slightlib.cihigh)
          
          # handcode again (same prob as with moderates)
          slightlib.cihigh[[2]] <- "0.06"
          
          # extlib
          amce.extlib.CI <- ideo.table.ni.cis$amce.extlib.CI
          amce.extlib.CI <- gsub("\\[|\\]", "", amce.extlib.CI)
          extlib.cilow <- substr(amce.extlib.CI, 1, 5)
          extlib.cilow <- gsub("\\,", "", extlib.cilow)
          extlib.cihigh <- substr(amce.extlib.CI, 6, 11)
          extlib.cihigh <- gsub("\\,", "", extlib.cihigh)
          
          # save to merge with reshaped data
          amce.cihigh <- cbind(extcon.cihigh, slightcon.cihigh, moderate.cihigh, 
                               slightlib.cihigh, extlib.cihigh)
          amce.cilow <- cbind(extcon.cilow, slightcon.cilow, moderate.cilow, 
                              slightlib.cilow, extlib.cilow)
          ideo.table.ni.new <- cbind(figA6D, amce.cihigh, amce.cilow)
          
          # get rid of the pasted CIs now
          ideo.table.ni.new <- subset(ideo.table.ni.new, select = -c(amce.extcons.CI, amce.slightcons.CI, 
                                                                     amce.moderate.CI, amce.slightlib.CI, 
                                                                     amce.extlib.CI))
          
          
          fA6Dlong <- melt(as.data.table(ideo.table.ni.new), id = 'amce.ideo.attributes', value.name = "value")
          
          # create index vectors to sort by
          fA6Dlong$order_atts <- rep(c(3, 4, 1, 2), times = 15)
          fA6Dlong$order_ideo <- rep(c(rep(5, times = 4), rep(4, times = 4), rep(3, times = 4),
                                       rep(2, times = 4), rep(1, times = 4)), times = 3)
          
          fA6Dlong <- fA6Dlong[order(fA6Dlong$order_atts, fA6Dlong$order_ideo),]
          
          fA6Dlong$ideology <- rep(c(rep("ext lib", times = 3), rep("slight lib", times = 3),
                                     rep("moderate", times = 3), rep("slight cons", times = 3),
                                     rep("ext cons", times = 3)), times = 4)
          
          fA6Dlong$stat <- rep(c("estimate", "ci.high", "ci.low"), times = 20)
          
          fA6Dlongtmp <- subset(fA6Dlong, select = c(amce.ideo.attributes, value, ideology, stat))
          fA6Dwide <- dcast(fA6Dlongtmp, amce.ideo.attributes + ideology ~ stat, value.var = "value")
          
          # now just re-sort fA6Dwide to get it to go from ext cons to ext lib again and in attribute order.
          fA6Dwide$ideologyorder <- rep(c(1, 5, 3, 2, 4), times = 4)
          fA6Dwide$attributeorder <- c(rep(2, times = 5), rep(4, times = 5), rep(3, times = 5), rep(1, times = 5))
          fA6Dwide <- fA6Dwide[order(fA6Dwide$attributeorder, fA6Dwide$ideologyorder),]
          fA6Dwide$reorder <- index(1:length(fA6Dwide$ideologyorder))
          fA6Dwide$addtoy <- c(0, 0, 0, 0, 0, .3, .3, .3, .3, .3, .7, .7, .7, .7, .7, .9, .9, .9, .9, .9)
          fA6Dwide$ci.low <- as.numeric(fA6Dwide$ci.low)
          fA6Dwide$ci.high <- as.numeric(fA6Dwide$ci.high)
          fA6Dwide$pch <- rep(c(1, 18, 17, 16, 0), times = 4)
          
          #### plot fig A6D ----
        
          # plot initialization
          par(mar=c(5, 10, 4, 2))
          plot(0,0 , xlim=c(-0.3, 0.25) , ylim=c(0,5.5), col="white" , yaxt="n" , ylab="", 
               xlab="", xaxt = "n")
          
          # fill the graph
          for (i in 1:length(fA6Dwide$estimate)){
            points(x=fA6Dwide$estimate[i], y=((fA6Dwide$reorder[i]*0.1)+(fA6Dwide$reorder[i]/8) + 
                                                fA6Dwide$addtoy[i]), pch = fA6Dwide$pch[i])
            
            segments(x0 = fA6Dwide$ci.low[i], y0 = ((fA6Dwide$reorder[i]*0.1)+(fA6Dwide$reorder[i]/8)+ 
                                                      fA6Dwide$addtoy[i]), 
                     x1 = fA6Dwide$ci.high[i], y1 = ((fA6Dwide$reorder[i]*0.1)+(fA6Dwide$reorder[i]/8)+ 
                                                       fA6Dwide$addtoy[i]))
          }
          #  add vertical line at 0
          segments(x0 = 0, x1 = 0, y0 = 0, y1 = 5.5, lty = 2, col = "black")
          
          # add / format axes
          axis(2, at = c(0.5, 2, 3.5, 4.9), labels = unique(fA6Dwide$amce.ideo.attributes), 
               tick = TRUE, col = "black", las = 1, cex.axis = 0.8)
          axis(1, at = c(-0.2, -0.1, 0.0, 0.1, 0.2), tick = TRUE, 
               cex.axis = 0.8)
          
          legend("topright", legend = c("Extremely Liberal", "Liberal or Slightly Lib.", "Moderate or don't know",  
                                        "Conservative or Slightly Cons.", "Extremely Conservative"), 
                 pch = c(0, 16, 17, 18, 1), 
                 title = "Ideology", cex = 0.5, ncol = 1)
          
          mtext("Change in E[Y]", side = 1, line=2, cex = 0.7, at = c(0,0))
          
          
# Study 2 Trustworthiness Results in discussion section and Appendix 10B----
          
          myresults_trust <- amce(trustrating ~ `CF Compliance` + `Grasp Issues` + Persuasiveness + 
                              `Professional Background` +
                              `Dark Money` + `Small Donors` + `Total amount raised` +
                              `Police Immigrant` + `Sex Ed`, data = datasmall, 
                            respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                            baselines = baselines)
          
          group.order <- c( "Dark Money", "CF Compliance", 
                            "Persuasiveness", "Grasp Issues", 
                            "Total amount raised", "Small Donors", "Professional Background",
                            "Police Immigrant", "Sex Ed")
          
        
          plot(myresults_trust, main = "", xlab = "Change in Trust Rating (1-5)",
               ci = 0.95, colors = NULL, 
               xlim = NULL, breaks = NULL,
               labels = NULL, attribute.names = NULL,
               level.names = NULL,
               label.baseline = TRUE, text.size = 8, text.color = "black",
               point.size = 0.5, dodge.size = 0.9, plot.theme = NULL,
               plot.display = "all",
               facet.names = NULL, facet.levels = NULL,
               group.order = group.order,
               legend.position="none")

          myresults_trust_noind <- amce(trustrating ~ `CF Compliance` + `Grasp Issues` + Persuasiveness + 
                                          `Professional Background` +
                                          `Dark Money` + `Small Donors` + `Total amount raised` +
                                          `Police Immigrant` + `Sex Ed`, data = datasmall2, 
                                        respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                                        baselines = baselines)
          
          group.order <- c( "Dark Money", "CF Compliance", 
                            "Persuasiveness", "Grasp Issues", 
                            "Total amount raised", "Small Donors", "Professional Background",
                            "Police Immigrant", "Sex Ed")
          
    ## Appendix Figure A6E ----
      plot(myresults_trust_noind, main = "", xlab = "Change in Trust Rating (1-5)",
            ci = 0.95, colors = NULL, 
            xlim = NULL, breaks = NULL,
            labels = NULL, attribute.names = NULL,
            level.names = NULL,
            label.baseline = TRUE, text.size = 8, text.color = "black",
            point.size = 0.5, dodge.size = 0.9, plot.theme = NULL,
            plot.display = "all",
            facet.names = NULL, facet.levels = NULL,
            group.order = group.order,
            legend.position="none")
          
## Claim in text: In Study 2, noncompliance caused a decrease in a five-point  ----
          # trustworthiness rating (-0.24 [-0.29, -0.19] for missed deadlines, 
          # and -0.28 [-0.33, -0.23] for reporting gaps). 
          
          
## Claim in text: Discouraging dark money causes an increase in the rating of ----
          # 0.13 [0.10, 0.17]. Over-compliance causes an increase in trust 
          # rating (0.04 [-0.01, 0.09] for including the percent of small donors 
          # on the website, and 0.04 [0.00, 0.09] for a map of donor locations)  

        summary(myresults_trust)


# Appendix Table A11 - Results by political engagement and ideology ----
        
        data$`deeply engaged` <- NA
        data$`deeply engaged`[data$newsint == "Most of the time"] <- 1
        data$`deeply engaged`[data$newsint == "Hardly at all" | data$newsint == "Only now and then" |
                                data$newsint == "Some of the time"] <- 0
        
        datasmall <- cbind(data$`deeply engaged`, datasmall)
        names(datasmall)[1] <- "Deeply Engaged"
        
        datasmallnews <- subset(datasmall, !(is.na(`Deeply Engaged`)))
        datasmallnews$`Deeply Engaged` <- as.factor(datasmallnews$`Deeply Engaged`)
        
        ## Now as a side-by-side appendix version of Fig 8, where Fig 8 is split  ####
        # into analysis with and without deep engagement
        
        
        # get these levels on the same plot -- extreme cons, cons, mod, lib, extreme lib
        # must run separately and then plot in one matrix.
        datasmallnews$ideoextreme <- NA
        datasmallnews$ideoextreme[datasmallnews$ideo7 == "Extremely Conservative"] <- "Extremely Conservative"
        datasmallnews$ideoextreme[datasmallnews$ideo7 == "Conservative" | 
                                    datasmallnews$ideo7 == "Slightly Conservative"] <- "(Slightly) Conservative"
        datasmallnews$ideoextreme[datasmallnews$ideo7 == "Moderate; Middle of the Road" | 
                                    datasmallnews$ideo7 == "Haven't thought much about this"] <- "Moderate or don't know"
        datasmallnews$ideoextreme[datasmallnews$ideo7 == "Liberal" | 
                                    datasmallnews$ideo7 == "Slightly Liberal"] <- "(Slightly) Liberal"
        datasmallnews$ideoextreme[datasmallnews$ideo7 == "Extremely Liberal"] <- "Extremely Liberal"
        datasmallnews$ideoextreme <- as.factor(datasmallnews$ideoextreme)
        d8 <- subset(datasmallnews, !(is.na(ideoextreme)))
        
        d8hi <- subset(d8, `Deeply Engaged` == 1)
        d8lo <- subset(d8, `Deeply Engaged` == 0)
        
        # first do the high engagement plots
        d8hi_ideo_extlib <- subset(d8hi, ideoextreme == "Extremely Liberal")
        results_d8hi_extlib <- amce(chosen ~  `Grasp Issues` + Persuasiveness + 
                                      `Professional Background` +
                                      `Dark Money` + `Small Donors` + `Total amount raised` +
                                      `Police Immigrant` + `Sex Ed` + `CF Pooled Compliance`, data = d8hi_ideo_extlib, 
                                    respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                                    baselines = baselines2)
        
        d8hi_ideo_extcons <- subset(d8hi, ideoextreme == "Extremely Conservative")
        results_d8hi_extcons <- amce(chosen ~  `Grasp Issues` + Persuasiveness + 
                                       `Professional Background` +
                                       `Dark Money` + `Small Donors` + `Total amount raised` +
                                       `Police Immigrant` + `Sex Ed` + `CF Pooled Compliance`, data = d8hi_ideo_extcons, 
                                     respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                                     baselines = baselines2)
        
        d8hi_ideo_lib <- subset(d8hi, ideoextreme == "(Slightly) Liberal")
        results_d8hi_lib <- amce(chosen ~  `Grasp Issues` + Persuasiveness + 
                                   `Professional Background` +
                                   `Dark Money` + `Small Donors` + `Total amount raised` +
                                   `Police Immigrant` + `Sex Ed` + `CF Pooled Compliance`, data = d8hi_ideo_lib, 
                                 respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                                 baselines = baselines2)
        
        d8hi_ideo_cons <- subset(d8hi, ideoextreme == "(Slightly) Conservative")
        results_d8hi_cons <- amce(chosen ~  `Grasp Issues` + Persuasiveness + 
                                    `Professional Background` +
                                    `Dark Money` + `Small Donors` + `Total amount raised` +
                                    `Police Immigrant` + `Sex Ed` + `CF Pooled Compliance`, data = d8hi_ideo_cons, 
                                  respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                                  baselines = baselines2)
        
        
        d8hi_ideo_mod <- subset(d8hi, ideoextreme == "Moderate or don't know")
        results_d8hi_mod <- amce(chosen ~  `Grasp Issues` + Persuasiveness + 
                                   `Professional Background` +
                                   `Dark Money` + `Small Donors` + `Total amount raised` +
                                   `Police Immigrant` + `Sex Ed` + `CF Pooled Compliance`, data = d8hi_ideo_mod, 
                                 respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                                 baselines = baselines2)
        
        
        # repeating with low engagement
        
        d8lo_ideo_extlib <- subset(d8lo, ideoextreme == "Extremely Liberal")
        results_d8lo_extlib <- amce(chosen ~  `Grasp Issues` + Persuasiveness + 
                                      `Professional Background` +
                                      `Dark Money` + `Small Donors` + `Total amount raised` +
                                      `Police Immigrant` + `Sex Ed` + `CF Pooled Compliance`, data = d8lo_ideo_extlib, 
                                    respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                                    baselines = baselines2)
        
        d8lo_ideo_extcons <- subset(d8lo, ideoextreme == "Extremely Conservative")
        results_d8lo_extcons <- amce(chosen ~  `Grasp Issues` + Persuasiveness + 
                                       `Professional Background` +
                                       `Dark Money` + `Small Donors` + `Total amount raised` +
                                       `Police Immigrant` + `Sex Ed` + `CF Pooled Compliance`, data = d8lo_ideo_extcons, 
                                     respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                                     baselines = baselines2)
        
        d8lo_ideo_lib <- subset(d8lo, ideoextreme == "(Slightly) Liberal")
        results_d8lo_lib <- amce(chosen ~  `Grasp Issues` + Persuasiveness + 
                                   `Professional Background` +
                                   `Dark Money` + `Small Donors` + `Total amount raised` +
                                   `Police Immigrant` + `Sex Ed` + `CF Pooled Compliance`, data = d8lo_ideo_lib, 
                                 respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                                 baselines = baselines2)
        
        d8lo_ideo_cons <- subset(d8lo, ideoextreme == "(Slightly) Conservative")
        results_d8lo_cons <- amce(chosen ~  `Grasp Issues` + Persuasiveness + 
                                    `Professional Background` +
                                    `Dark Money` + `Small Donors` + `Total amount raised` +
                                    `Police Immigrant` + `Sex Ed` + `CF Pooled Compliance`, data = d8lo_ideo_cons, 
                                  respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                                  baselines = baselines2)
        
        
        d8lo_ideo_mod <- subset(d8lo, ideoextreme == "Moderate or don't know")
        results_d8lo_mod <- amce(chosen ~  `Grasp Issues` + Persuasiveness + 
                                   `Professional Background` +
                                   `Dark Money` + `Small Donors` + `Total amount raised` +
                                   `Police Immigrant` + `Sex Ed` + `CF Pooled Compliance`, data = d8lo_ideo_mod, 
                                 respondent.id = "ParticipantID", cluster = TRUE, design=design, 
                                 baselines = baselines2)
        
        
        # Appendix Table A11
        
        d8hi.extcons.est <- round(summary(results_d8hi_extcons)$amce$Estimate, digits = 2)
        d8hi.extcons.SE.cilow <- round(summary(results_d8hi_extcons)$amce$Estimate, digits = 2) - 
          2*(summary(results_d8hi_extcons)$amce$`Std. Err`)
        d8hi.extcons.SE.cihigh <- round(summary(results_d8hi_extcons)$amce$Estimate, digits = 2) +
          2*(summary(results_d8hi_extcons)$amce$`Std. Err`)
        d8hi.extcons.CI.part1 <- paste("[", round(d8hi.extcons.SE.cilow, digits = 2), sep = "")
        d8hi.extcons.CI.part2 <- paste(round(d8hi.extcons.SE.cihigh, digits = 2), "]", sep = "")
        d8hi.extcons.CI <- paste(d8hi.extcons.CI.part1, d8hi.extcons.CI.part2, sep = ",")
        
        
        d8hi.cons.est <- round(summary(results_d8hi_cons)$amce$Estimate, digits = 2)
        d8hi.cons.SE.cilow <- round(summary(results_d8hi_cons)$amce$Estimate, digits = 2) - 
          2*(summary(results_d8hi_cons)$amce$`Std. Err`)
        d8hi.cons.SE.cihigh <- round(summary(results_d8hi_cons)$amce$Estimate, digits = 2) +
          2*(summary(results_d8hi_cons)$amce$`Std. Err`)
        d8hi.cons.CI.part1 <- paste("[", round(d8hi.cons.SE.cilow, digits = 2), sep = "")
        d8hi.cons.CI.part2 <- paste(round(d8hi.cons.SE.cihigh, digits = 2), "]", sep = "")
        d8hi.cons.CI <- paste(d8hi.cons.CI.part1, d8hi.cons.CI.part2, sep = ",")
        
        
        d8hi.extlib.est <- round(summary(results_d8hi_extlib)$amce$Estimate, digits = 2)
        d8hi.extlib.SE.cilow <- round(summary(results_d8hi_extlib)$amce$Estimate, digits = 2) - 
          2*(summary(results_d8hi_extlib)$amce$`Std. Err`)
        d8hi.extlib.SE.cihigh <- round(summary(results_d8hi_extlib)$amce$Estimate, digits = 2) +
          2*(summary(results_d8hi_extlib)$amce$`Std. Err`)
        d8hi.extlib.CI.part1 <- paste("[", round(d8hi.extlib.SE.cilow, digits = 2), sep = "")
        d8hi.extlib.CI.part2 <- paste(round(d8hi.extlib.SE.cihigh, digits = 2), "]", sep = "")
        d8hi.extlib.CI <- paste(d8hi.extlib.CI.part1, d8hi.extlib.CI.part2, sep = ",")
        
        
        d8hi.lib.est <- round(summary(results_d8hi_lib)$amce$Estimate, digits = 2)
        d8hi.lib.SE.cilow <- round(summary(results_d8hi_lib)$amce$Estimate, digits = 2) - 
          2*(summary(results_d8hi_lib)$amce$`Std. Err`)
        d8hi.lib.SE.cihigh <- round(summary(results_d8hi_lib)$amce$Estimate, digits = 2) +
          2*(summary(results_d8hi_lib)$amce$`Std. Err`)
        d8hi.lib.CI.part1 <- paste("[", round(d8hi.lib.SE.cilow, digits = 2), sep = "")
        d8hi.lib.CI.part2 <- paste(round(d8hi.lib.SE.cihigh, digits = 2), "]", sep = "")
        d8hi.lib.CI <- paste(d8hi.lib.CI.part1, d8hi.lib.CI.part2, sep = ",")
        
        
        
        d8hi.mod.est <- round(summary(results_d8hi_mod)$amce$Estimate, digits = 2)
        d8hi.mod.SE.cilow <- round(summary(results_d8hi_mod)$amce$Estimate, digits = 2) - 
          2*(summary(results_d8hi_mod)$amce$`Std. Err`)
        d8hi.mod.SE.cihigh <- round(summary(results_d8hi_mod)$amce$Estimate, digits = 2) +
          2*(summary(results_d8hi_mod)$amce$`Std. Err`)
        d8hi.mod.CI.part1 <- paste("[", round(d8hi.mod.SE.cilow, digits = 2), sep = "")
        d8hi.mod.CI.part2 <- paste(round(d8hi.mod.SE.cihigh, digits = 2), "]", sep = "")
        d8hi.mod.CI <- paste(d8hi.mod.CI.part1, d8hi.mod.CI.part2, sep = ",")
        
        
        
        d8lo.extcons.est <- round(summary(results_d8lo_extcons)$amce$Estimate, digits = 2)
        d8lo.extcons.SE.cilow <- round(summary(results_d8lo_extcons)$amce$Estimate, digits = 2) - 
          2*(summary(results_d8lo_extcons)$amce$`Std. Err`)
        d8lo.extcons.SE.cihigh <- round(summary(results_d8lo_extcons)$amce$Estimate, digits = 2) +
          2*(summary(results_d8lo_extcons)$amce$`Std. Err`)
        d8lo.extcons.CI.part1 <- paste("[", round(d8lo.extcons.SE.cilow, digits = 2), sep = "")
        d8lo.extcons.CI.part2 <- paste(round(d8lo.extcons.SE.cihigh, digits = 2), "]", sep = "")
        d8lo.extcons.CI <- paste(d8lo.extcons.CI.part1, d8lo.extcons.CI.part2, sep = ",")
        
        
        d8lo.cons.est <- round(summary(results_d8lo_cons)$amce$Estimate, digits = 2)
        d8lo.cons.SE.cilow <- round(summary(results_d8lo_cons)$amce$Estimate, digits = 2) - 
          2*(summary(results_d8lo_cons)$amce$`Std. Err`)
        d8lo.cons.SE.cihigh <- round(summary(results_d8lo_cons)$amce$Estimate, digits = 2) +
          2*(summary(results_d8lo_cons)$amce$`Std. Err`)
        d8lo.cons.CI.part1 <- paste("[", round(d8lo.cons.SE.cilow, digits = 2), sep = "")
        d8lo.cons.CI.part2 <- paste(round(d8lo.cons.SE.cihigh, digits = 2), "]", sep = "")
        d8lo.cons.CI <- paste(d8lo.cons.CI.part1, d8lo.cons.CI.part2, sep = ",")
        
        
        d8lo.extlib.est <- round(summary(results_d8lo_extlib)$amce$Estimate, digits = 2)
        d8lo.extlib.SE.cilow <- round(summary(results_d8lo_extlib)$amce$Estimate, digits = 2) - 
          2*(summary(results_d8lo_extlib)$amce$`Std. Err`)
        d8lo.extlib.SE.cihigh <- round(summary(results_d8lo_extlib)$amce$Estimate, digits = 2) +
          2*(summary(results_d8lo_extlib)$amce$`Std. Err`)
        d8lo.extlib.CI.part1 <- paste("[", round(d8lo.extlib.SE.cilow, digits = 2), sep = "")
        d8lo.extlib.CI.part2 <- paste(round(d8lo.extlib.SE.cihigh, digits = 2), "]", sep = "")
        d8lo.extlib.CI <- paste(d8lo.extlib.CI.part1, d8lo.extlib.CI.part2, sep = ",")
        
        
        d8lo.lib.est <- round(summary(results_d8lo_lib)$amce$Estimate, digits = 2)
        d8lo.lib.SE.cilow <- round(summary(results_d8lo_lib)$amce$Estimate, digits = 2) - 
          2*(summary(results_d8lo_lib)$amce$`Std. Err`)
        d8lo.lib.SE.cihigh <- round(summary(results_d8lo_lib)$amce$Estimate, digits = 2) +
          2*(summary(results_d8lo_lib)$amce$`Std. Err`)
        d8lo.lib.CI.part1 <- paste("[", round(d8lo.lib.SE.cilow, digits = 2), sep = "")
        d8lo.lib.CI.part2 <- paste(round(d8lo.lib.SE.cihigh, digits = 2), "]", sep = "")
        d8lo.lib.CI <- paste(d8lo.lib.CI.part1, d8lo.lib.CI.part2, sep = ",")
        
        
        
        d8lo.mod.est <- round(summary(results_d8lo_mod)$amce$Estimate, digits = 2)
        d8lo.mod.SE.cilow <- round(summary(results_d8lo_mod)$amce$Estimate, digits = 2) - 
          2*(summary(results_d8lo_mod)$amce$`Std. Err`)
        d8lo.mod.SE.cihigh <- round(summary(results_d8lo_mod)$amce$Estimate, digits = 2) +
          2*(summary(results_d8lo_mod)$amce$`Std. Err`)
        d8lo.mod.CI.part1 <- paste("[", round(d8lo.mod.SE.cilow, digits = 2), sep = "")
        d8lo.mod.CI.part2 <- paste(round(d8lo.mod.SE.cihigh, digits = 2), "]", sep = "")
        d8lo.mod.CI <- paste(d8lo.mod.CI.part1, d8lo.mod.CI.part2, sep = ",")
        
        
        d8.attributes <- summary(results_d8hi_cons)$amce$Level
        d8.attributes_top <- summary(results_d8hi_cons)$amce$Attribute
        
        ideo.engaged.table <- as.data.frame(cbind(d8.attributes_top, d8.attributes,
                                                  d8lo.extcons.est, d8lo.extcons.CI, 
                                                  d8lo.cons.est, d8lo.cons.CI, 
                                                  d8lo.mod.est, d8lo.mod.CI, 
                                                  d8lo.lib.est, d8lo.lib.CI, 
                                                  d8lo.extlib.est, d8lo.extlib.CI,
                                                  d8hi.extcons.est, d8hi.extcons.CI, 
                                                  d8hi.cons.est, d8hi.cons.CI, 
                                                  d8hi.mod.est, d8hi.mod.CI, 
                                                  d8hi.lib.est, d8hi.lib.CI, 
                                                  d8hi.extlib.est, d8hi.extlib.CI))
        
       # this is tied to the following claim in the text:  
        # "Similarly, engagement with political news may help explain some of 
        # these effects. In Appendix 11, readers can observe that high news engagement 
        # respondents drive the reward for discouraging dark money"
        ideo.engaged.table
        